// SPDX-License-Identifier: GPL-2.0-or-later
/* -*- mode: c; c-basic-offset: 8; -*-
 *
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * Copyright (C) 2004 Oracle.  All rights reserved.
 *
 * ----
 *
 * Callers for this were originally written against a very simple synchronus
 * API.  This implementation reflects those simple callers.  Some day I'm sure
 * we'll need to move to a more robust posting/callback mechanism.
 *
 * Transmit calls pass in kernel virtual addresses and block copying this into
 * the socket's tx buffers via a usual blocking sendmsg.  They'll block waiting
 * for a failed socket to timeout.  TX callers can also pass in a poniter to an
 * 'int' which gets filled with an errno off the wire in response to the
 * message they send.
 *
 * Handlers for unsolicited messages are registered.  Each socket has a page
 * that incoming data is copied into.  First the header, then the data.
 * Handlers are called from only one thread with a reference to this per-socket
 * page.  This page is destroyed after the handler call, so it can't be
 * referenced beyond the call.  Handlers may block but are discouraged from
 * doing so.
 *
 * Any framing errors (bad magic, large payload lengths) close a connection.
 *
 * Our sock_container holds the state we associate with a socket.  It's current
 * framing state is held there as well as the refcounting we do around when it
 * is safe to tear down the socket.  The socket is only finally torn down from
 * the container when the container loses all of its references -- so as long
 * as you hold a ref on the container you can trust that the socket is valid
 * for use with kernel socket APIs.
 *
 * Connections are initiated between a pair of nodes when the node with the
 * higher node number gets a heartbeat callback which indicates that the lower
 * numbered node has started heartbeating.  The lower numbered node is passive
 * and only accepts the connection if the higher numbered node is heartbeating.
 */

#include <linux/kernel.h>
#include <linux/sched/mm.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include <linux/kref.h>
#include <linux/net.h>
#include <linux/crc16.h>
#include <net/tcp.h>
#include <crypto/hash.h>
#include <crypto/sha1.h>
#include <crypto/sha2.h>
#include <crypto/sha3.h>
#include <linux/uaccess.h>

#include "heartbeat.h"
#include "tcp.h"
#include "nodemanager.h"
#define MLOG_MASK_PREFIX ML_TCP
#include "masklog.h"
#include "quorum.h"

#include "tcp_internal.h"

#define UNPACK(...) __VA_ARGS__
#define SC_NODEF_FMT "node %s (num %u) at %pI4:%u"
#define SC_NODEF_ARGS(sc) UNPACK(sc->sc_node->nd_name, sc->sc_node->nd_num,	\
			  &sc->sc_node->nd_ipv4_address,		\
			  ntohs(sc->sc_node->nd_ip_port))

#define SC_PRE_NODEF_ARGS(sc) UNPACK(sc->sc_node->nd_name, sc->sc_node->nd_num,	\
			  &sc->sc_node->nd_ipv4_address,		\
			  (ntohs(sc->sc_node->nd_ip_port) + 1))

#define SC_IPV6_NODEF_FMT "node %s (num %u) at %pI6:%u"
#define SC_IPV6_NODEF_ARGS(sc) UNPACK(sc->sc_node->nd_name, sc->sc_node->nd_num,	\
			  sc->sc_node->nd_ipv6_address,		\
			  ntohs(sc->sc_node->nd_ip_port))

#define SC_IPV6_PRE_NODEF_ARGS(sc) UNPACK(sc->sc_node->nd_name, sc->sc_node->nd_num,	\
			  sc->sc_node->nd_ipv6_address,		\
			  (ntohs(sc->sc_node->nd_ip_port) + 1))

/*
 * In the following two log macros, the whitespace after the ',' just
 * before ##args is intentional. Otherwise, gcc 2.95 will eat the
 * previous token if args expands to nothing.
 */
#define msglog(hdr, fmt, args...) do {					\
	typeof(hdr) __hdr = (hdr);					\
	mlog(ML_MSG, "o2net: [mag %u len %u typ %u stat %d sys_stat %d "	\
	     "key %08x num %u] " fmt,					\
	     be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), 	\
	     be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status),	\
	     be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key),	\
	     be32_to_cpu(__hdr->msg_num) ,  ##args);			\
} while (0)

#define msglog_level(level, hdr, fmt, args...) do {						\
	typeof(hdr) __hdr = (hdr);					\
	mlog(level, "o2net: [magic %u len %u typ %u crc %u sys_stat %u "	\
	     "stat %u key %u msg_num %u ack_num %llu seq_num %llu] " fmt,	\
	     be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len),		\
	     be16_to_cpu(__hdr->msg_type), be16_to_cpu(__hdr->crc),		\
	     be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->status),	\
	     be32_to_cpu(__hdr->key), be32_to_cpu(__hdr->msg_num),		\
	     be64_to_cpu(__hdr->ack_num), be64_to_cpu(__hdr->seq_num),	\
	     ##args);	\
} while (0)

#define sclog(sc, fmt, args...) do {					\
	typeof(sc) __sc = (sc);						\
	mlog(ML_SOCKET, "o2net: [sc %p refs %d sock %p node %u page %p "	\
	     "pg_off %zu] " fmt, __sc,					\
	     kref_read(&__sc->sc_kref), __sc->sc_sock,  \
	    __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off ,	\
	    ##args);							\
} while (0)

static DEFINE_RWLOCK(o2net_handler_lock);
static struct rb_root o2net_handler_tree = RB_ROOT;

static struct o2net_node o2net_nodes[O2NM_MAX_NODES];

/* XXX someday we'll need better accounting */
static struct socket *o2net_listen_sock;

/*
 * listen work is only queued by the listening socket callbacks on the
 * o2net_wq.  teardown detaches the callbacks before destroying the workqueue.
 * quorum work is queued as sock containers are shutdown.. stop_listening
 * tears down all the node's sock containers, preventing future shutdowns
 * and queued quroum work, before canceling delayed quorum work and
 * destroying the work queue.
 */
static struct workqueue_struct *o2net_wq;
static struct work_struct o2net_listen_work;

static struct o2hb_callback_func o2net_hb_up, o2net_hb_down;
#define O2NET_HB_PRI 0x1

static struct o2net_handshake *o2net_hand;
static struct o2net_handshake_token *o2net_hand_token;
static struct o2net_msg *o2net_keep_req, *o2net_keep_resp;

static int o2net_sys_err_translations[O2NET_ERR_MAX] =
		{[O2NET_ERR_NONE]	= 0,
		 [O2NET_ERR_NO_HNDLR]	= -ENOPROTOOPT,
		 [O2NET_ERR_OVERFLOW]	= -EOVERFLOW,
		 [O2NET_ERR_LIMITATION]	= -ENOPROTOOPT,
		 [O2NET_ERR_DIED]	= -EHOSTDOWN,};

static struct socket *o2net_pre_listen_sock;
static struct o2net_node o2net_pre_nodes[O2NM_MAX_NODES];
static struct workqueue_struct *o2net_pre_wq;
static struct work_struct o2net_pre_listen_work;
static struct kmem_cache *o2net_msg_cache;
static struct kmem_cache *o2net_ack_num_cache;
struct delayed_work del_seq_status_wq;
atomic_t del_seq_status_state;

/* can't quite avoid *all* internal declarations :/ */
static void o2net_sc_connect_completed(struct work_struct *work);
static void o2net_rx_until_empty(struct work_struct *work);
static void o2net_shutdown_sc(struct work_struct *work);
static void o2net_listen_data_ready(struct sock *sk);
static void o2net_sc_send_keep_req(struct work_struct *work);
static void o2net_idle_timer(struct timer_list *t);
static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc);
static void o2net_pre_rx_until_empty(struct work_struct *work);
static void o2net_pre_shutdown_sc(struct work_struct *work);
static void o2net_pre_listen_data_ready(struct sock *sk);
static void o2net_pre_idle_timer(struct timer_list *t);
static void o2net_pre_sc_postpone_idle(struct o2net_sock_container *sc);
static void o2net_pre_sc_reset_idle_timer(struct o2net_sock_container *sc);
static void o2net_pre_sc_connect_completed(struct work_struct *work);
static u64 o2net_get_ack_num(struct o2net_node *nn, u64 seq_num, u32 key);

#ifdef CONFIG_DEBUG_FS
static void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype,
			   u32 msgkey, struct task_struct *task, u16 node)
{
	INIT_LIST_HEAD(&nst->st_net_debug_item);
	nst->st_task = task;
	nst->st_msg_type = msgtype;
	nst->st_msg_key = msgkey;
	nst->st_node = node;
}

static inline void o2net_set_nst_sock_time(struct o2net_send_tracking *nst)
{
	nst->st_sock_time = ktime_get();
}

static inline void o2net_set_nst_send_time(struct o2net_send_tracking *nst)
{
	nst->st_send_time = ktime_get();
}

static inline void o2net_set_nst_status_time(struct o2net_send_tracking *nst)
{
	nst->st_status_time = ktime_get();
}

static inline void o2net_set_nst_sock_container(struct o2net_send_tracking *nst,
						struct o2net_sock_container *sc)
{
	nst->st_sc = sc;
}

static inline void o2net_set_nst_msg_id(struct o2net_send_tracking *nst,
					u32 msg_id)
{
	nst->st_id = msg_id;
}

static inline void o2net_set_sock_timer(struct o2net_sock_container *sc)
{
	sc->sc_tv_timer = ktime_get();
}

static inline void o2net_set_data_ready_time(struct o2net_sock_container *sc)
{
	sc->sc_tv_data_ready = ktime_get();
}

static inline void o2net_set_advance_start_time(struct o2net_sock_container *sc)
{
	sc->sc_tv_advance_start = ktime_get();
}

static inline void o2net_set_advance_stop_time(struct o2net_sock_container *sc)
{
	sc->sc_tv_advance_stop = ktime_get();
}

static inline void o2net_set_func_start_time(struct o2net_sock_container *sc)
{
	sc->sc_tv_func_start = ktime_get();
}

static inline void o2net_set_func_stop_time(struct o2net_sock_container *sc)
{
	sc->sc_tv_func_stop = ktime_get();
}

#else  /* CONFIG_DEBUG_FS */
# define o2net_init_nst(a, b, c, d, e)
# define o2net_set_nst_sock_time(a)
# define o2net_set_nst_send_time(a)
# define o2net_set_nst_status_time(a)
# define o2net_set_nst_sock_container(a, b)
# define o2net_set_nst_msg_id(a, b)
# define o2net_set_sock_timer(a)
# define o2net_set_data_ready_time(a)
# define o2net_set_advance_start_time(a)
# define o2net_set_advance_stop_time(a)
# define o2net_set_func_start_time(a)
# define o2net_set_func_stop_time(a)
#endif /* CONFIG_DEBUG_FS */

#ifdef CONFIG_OCFS2_FS_STATS
static ktime_t o2net_get_func_run_time(struct o2net_sock_container *sc)
{
	return ktime_sub(sc->sc_tv_func_stop, sc->sc_tv_func_start);
}

static void o2net_update_send_stats(struct o2net_send_tracking *nst,
				    struct o2net_sock_container *sc)
{
	sc->sc_tv_status_total = ktime_add(sc->sc_tv_status_total,
					   ktime_sub(ktime_get(),
						     nst->st_status_time));
	sc->sc_tv_send_total = ktime_add(sc->sc_tv_send_total,
					 ktime_sub(nst->st_status_time,
						   nst->st_send_time));
	sc->sc_tv_acquiry_total = ktime_add(sc->sc_tv_acquiry_total,
					    ktime_sub(nst->st_send_time,
						      nst->st_sock_time));
	sc->sc_send_count++;
}

static void o2net_update_recv_stats(struct o2net_sock_container *sc)
{
	sc->sc_tv_process_total = ktime_add(sc->sc_tv_process_total,
					    o2net_get_func_run_time(sc));
	sc->sc_recv_count++;
}

#else

# define o2net_update_send_stats(a, b)

# define o2net_update_recv_stats(sc)

#endif /* CONFIG_OCFS2_FS_STATS */

static inline unsigned int o2net_reconnect_delay(void)
{
	return o2nm_single_cluster->cl_reconnect_delay_ms;
}

static inline unsigned int o2net_keepalive_delay(void)
{
	return o2nm_single_cluster->cl_keepalive_delay_ms;
}

static inline unsigned int o2net_idle_timeout(void)
{
	return o2nm_single_cluster->cl_idle_timeout_ms;
}

static inline unsigned int o2net_quorum_delay_threshold(void)
{
	return o2nm_single_cluster->cl_quorum_delay_threshod;
}

static inline int o2net_sys_err_to_errno(enum o2net_system_error err)
{
	int trans;

	if (err >= O2NET_ERR_MAX) {
		mlog(ML_ERROR, "o2net: unexpected system errno %d\n", err);
		return -EAGAIN;
	}
	trans = o2net_sys_err_translations[err];

	/* Just in case we mess up the translation table above */
	BUG_ON(err != O2NET_ERR_NONE && trans == 0);
	return trans;
}

static struct o2net_node *o2net_nn_from_num(u16 node_num)
{
	BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes));
	return &o2net_nodes[node_num];
}

static u16 o2net_num_from_nn(struct o2net_node *nn)
{
	BUG_ON(nn == NULL);
	return nn - o2net_nodes;
}

static struct o2net_node *o2net_pre_nn_from_num(u16 node_num)
{
	BUG_ON(node_num >= ARRAY_SIZE(o2net_pre_nodes));
	return &o2net_pre_nodes[node_num];
}

static u16 o2net_pre_num_from_nn(struct o2net_node *nn)
{
	BUG_ON(nn == NULL);
	return nn - o2net_pre_nodes;
}

/* ------------------------------------------------------------ */

static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw)
{
	int ret;

	spin_lock(&nn->nn_lock);
	ret = nn->nn_sc_valid ? 0 : -ENOTCONN;
	spin_unlock(&nn->nn_lock);
	if (ret) {
		mlog(ML_CONN, "o2net: connection invalid while preparing nsw\n");
		return ret;
	}

	spin_lock(&nn->nn_lock);
	ret = idr_alloc(&nn->nn_status_idr, nsw, 0, 0, GFP_ATOMIC);
	if (ret >= 0) {
		nsw->ns_id = ret;
		list_add_tail(&nsw->ns_node_item, &nn->nn_status_list);
	}
	spin_unlock(&nn->nn_lock);
	if (ret < 0)
		return ret;

	init_waitqueue_head(&nsw->ns_wq);
	nsw->ns_sys_status = O2NET_ERR_NONE;
	nsw->ns_status = 0;
	return 0;
}

static void o2net_complete_nsw_locked(struct o2net_node *nn,
				      struct o2net_status_wait *nsw,
				      enum o2net_system_error sys_status,
				      s32 status)
{
	assert_spin_locked(&nn->nn_lock);

	if (!list_empty(&nsw->ns_node_item)) {
		list_del_init(&nsw->ns_node_item);
		nsw->ns_sys_status = sys_status;
		nsw->ns_status = status;
		idr_remove(&nn->nn_status_idr, nsw->ns_id);
		wake_up(&nsw->ns_wq);
	}
}

static int o2net_complete_nsw(struct o2net_node *nn,
			       struct o2net_status_wait *nsw,
			       u64 id, enum o2net_system_error sys_status,
			       s32 status)
{
	int ret = -ENOENT;

	spin_lock(&nn->nn_lock);
	if (nsw == NULL) {
		if (id > INT_MAX)
			goto out;

		nsw = idr_find(&nn->nn_status_idr, id);
		if (nsw == NULL)
			goto out;
	}

	o2net_complete_nsw_locked(nn, nsw, sys_status, status);
	ret = 0;

out:
	spin_unlock(&nn->nn_lock);
	return ret;
}

static void o2net_complete_nodes_nsw(struct o2net_node *nn)
{
	struct o2net_status_wait *nsw, *tmp;
	unsigned int num_kills = 0;

	assert_spin_locked(&nn->nn_lock);

	list_for_each_entry_safe(nsw, tmp, &nn->nn_status_list, ns_node_item) {
		o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
		num_kills++;
	}

	mlog(0, "o2net: completed %d messages for node %u\n", num_kills,
	     o2net_num_from_nn(nn));
}

static int o2net_nsw_completed(struct o2net_node *nn,
			       struct o2net_status_wait *nsw)
{
	int completed;
	spin_lock(&nn->nn_lock);
	completed = list_empty(&nsw->ns_node_item);
	spin_unlock(&nn->nn_lock);
	return completed;
}

static void o2net_pre_complete_nodes_nsw(struct o2net_node *nn)
{
	struct o2net_status_wait *nsw, *tmp;
	unsigned int num_kills = 0;

	assert_spin_locked(&nn->nn_lock);

	list_for_each_entry_safe(nsw, tmp, &nn->nn_status_list, ns_node_item) {
		o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
		num_kills++;
	}

	mlog(0, "o2net_pre: completed %d messages for node %u\n", num_kills,
			o2net_pre_num_from_nn(nn));
}

/* ------------------------------------------------------------ */

static void sc_kref_release(struct kref *kref)
{
	struct o2net_sock_container *sc = container_of(kref,
					struct o2net_sock_container, sc_kref);
	BUG_ON(timer_pending(&sc->sc_idle_timeout));

	sclog(sc, "releasing\n");

	if (sc->sc_sock) {
		sock_release(sc->sc_sock);
		sc->sc_sock = NULL;
	}

	o2nm_undepend_item(&sc->sc_node->nd_item);
	o2nm_node_put(sc->sc_node);
	sc->sc_node = NULL;

	o2net_debug_del_sc(sc);

	/*
	 * pages are alloced from budy system, so free them for budy system.
	 */
	if (sc->sc_page) {
		__free_pages(sc->sc_page, 2);
		sc->sc_page = NULL;
	}
	kfree(sc);
}

static void sc_put(struct o2net_sock_container *sc)
{
	sclog(sc, "put\n");
	kref_put(&sc->sc_kref, sc_kref_release);
}
static void sc_get(struct o2net_sock_container *sc)
{
	sclog(sc, "get\n");
	kref_get(&sc->sc_kref);
}
static struct o2net_sock_container *sc_alloc(struct o2nm_node *node)
{
	struct o2net_sock_container *sc, *ret = NULL;
	struct page *page = NULL;
	int status = 0;

	page = alloc_pages(GFP_NOFS, 2);
	sc = kzalloc(sizeof(*sc), GFP_NOFS);
	if (sc == NULL || page == NULL)
		goto out;

	kref_init(&sc->sc_kref);
	o2nm_node_get(node);
	sc->sc_node = node;

	/* pin the node item of the remote node */
	status = o2nm_depend_item(&node->nd_item);
	if (status) {
		mlog_errno(status);
		o2nm_node_put(node);
		goto out;
	}
	INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed);
	INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty);
	INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc);
	INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req);

	timer_setup(&sc->sc_idle_timeout, o2net_idle_timer, 0);

	sclog(sc, "alloced\n");

	ret = sc;
	sc->sc_page = page;
	o2net_debug_add_sc(sc);
	sc = NULL;
	page = NULL;

out:
	if (page)
		__free_pages(page, 2);
	kfree(sc);

	return ret;
}

static struct o2net_sock_container *sc_pre_alloc(struct o2nm_node *node)
{
	struct o2net_sock_container *sc, *ret = NULL;
	struct page *page = NULL;
	int status = 0;

	page = alloc_pages(GFP_NOFS, 2);
	sc = kzalloc(sizeof(*sc), GFP_NOFS);
	if (sc == NULL || page == NULL)
		goto out;

	kref_init(&sc->sc_kref);
	o2nm_node_get(node);
	sc->sc_node = node;

	/* pin the node item of the remote node */
	status = o2nm_depend_item(&node->nd_item);
	if (status) {
		mlog_errno(status);
		o2nm_node_put(node);
		goto out;
	}

	INIT_WORK(&sc->sc_pre_connect_work, o2net_pre_sc_connect_completed);
	INIT_WORK(&sc->sc_pre_shutdown_work, o2net_pre_shutdown_sc);
	INIT_WORK(&sc->sc_pre_rx_work, o2net_pre_rx_until_empty);

	timer_setup(&sc->sc_idle_timeout, o2net_pre_idle_timer, 0);

	sclog(sc, "alloced\n");

	ret = sc;
	sc->sc_page = page;
	o2net_debug_add_sc(sc);
	sc = NULL;
	page = NULL;

out:
	if (page)
		__free_pages(page, 2);
	kfree(sc);

	return ret;
}

/* ------------------------------------------------------------ */

static void o2net_pre_sc_queue_work(struct o2net_sock_container *sc,
		struct work_struct *work)
{
	sc_get(sc);
	if (!queue_work(o2net_pre_wq, work))
		sc_put(sc);
}

static void o2net_sc_queue_work(struct o2net_sock_container *sc,
				struct work_struct *work)
{
	sc_get(sc);
	if (!queue_work(o2net_wq, work))
		sc_put(sc);
}
static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc,
					struct delayed_work *work,
					int delay)
{
	sc_get(sc);
	if (!queue_delayed_work(o2net_wq, work, delay))
		sc_put(sc);
}
static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc,
					 struct delayed_work *work)
{
	if (cancel_delayed_work(work))
		sc_put(sc);
}

static atomic_t o2net_connected_peers = ATOMIC_INIT(0);

int o2net_num_connected_peers(void)
{
	return atomic_read(&o2net_connected_peers);
}

static void o2net_pre_set_nn_state(struct o2net_node *nn,
		struct o2net_sock_container *sc, unsigned int valid, int err)
{
	int was_valid = nn->nn_sc_valid;
	struct o2net_sock_container *old_sc = nn->nn_sc;

	assert_spin_locked(&nn->nn_lock);

	if (was_valid && !valid && err == 0)
		err = -ENOTCONN;

	mlog(ML_NOTICE, "o2net_pre: node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
			o2net_pre_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
			nn->nn_persistent_error, err);

	nn->nn_sc = sc;
	nn->nn_sc_valid = valid ? 1 : 0;
	nn->nn_persistent_error = err;

	/* mirrors o2net_tx_can_proceed() */
	if (nn->nn_persistent_error || nn->nn_sc_valid)
		wake_up(&nn->nn_sc_wq);

	if (was_valid && !valid) {
		if (old_sc) {
			if (old_sc->sc_node->nd_ipnet_type == IPV4_TYPE)
				mlog(ML_NOTICE, "o2net_pre: no longer connected to "
						SC_NODEF_FMT "\n", SC_PRE_NODEF_ARGS(old_sc));
			else
				mlog(ML_NOTICE, "o2net_pre: no longer connected to "
						SC_IPV6_NODEF_FMT "\n",
						SC_IPV6_PRE_NODEF_ARGS(old_sc));
		}
		o2net_pre_complete_nodes_nsw(nn);
	}

	if (!was_valid && valid) {
		/* when set up pre_connect, we cancel expired work */
		cancel_delayed_work(&nn->nn_pre_connect_expired);
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: %s " SC_NODEF_FMT "\n",
					o2nm_this_node() > sc->sc_node->nd_num ?
					"connected to" : "accepted connection from",
					SC_PRE_NODEF_ARGS(sc));
		else
			mlog(ML_NOTICE, "o2net_pre: %s " SC_IPV6_NODEF_FMT "\n",
					o2nm_this_node() > sc->sc_node->nd_num ?
					"connected to" : "accepted connection from",
					SC_IPV6_PRE_NODEF_ARGS(sc));
	}

	if (!valid && !was_valid && nn->nn_sc &&
			!nn->nn_persistent_error && o2net_pre_wq) {
		/* Only queue expired work when begin start connect */
		queue_delayed_work(o2net_pre_wq, &nn->nn_pre_connect_expired,
				msecs_to_jiffies(o2net_idle_timeout()));
	}
	/* keep track of the nn's sc ref for the caller */
	if ((old_sc == NULL) && sc)
		sc_get(sc);
	if (old_sc && (old_sc != sc)) {
		o2net_pre_sc_queue_work(old_sc, &old_sc->sc_pre_shutdown_work);
		sc_put(old_sc);
	}
}

static void o2net_set_nn_state(struct o2net_node *nn,
			       struct o2net_sock_container *sc,
			       unsigned valid, int err)
{
	int was_valid = nn->nn_sc_valid;
	int was_err = nn->nn_persistent_error;
	struct o2net_sock_container *old_sc = nn->nn_sc;

	assert_spin_locked(&nn->nn_lock);

	if (old_sc && !sc)
		atomic_dec(&o2net_connected_peers);
	else if (!old_sc && sc)
		atomic_inc(&o2net_connected_peers);

	/* the node num comparison and single connect/accept path should stop
	 * an non-null sc from being overwritten with another */
	BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
	mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
	mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);

	if (was_valid && !valid && err == 0)
		err = -ENOTCONN;

	mlog(ML_CONN, "o2net: node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
			o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
			nn->nn_persistent_error, err);

	nn->nn_sc = sc;
	nn->nn_sc_valid = valid ? 1 : 0;
	nn->nn_persistent_error = err;

	/* mirrors o2net_tx_can_proceed() */
	if (nn->nn_persistent_error || nn->nn_sc_valid)
		wake_up(&nn->nn_sc_wq);

	if (was_valid && !was_err && nn->nn_persistent_error) {
		o2quo_conn_err(o2net_num_from_nn(nn));
		queue_delayed_work(o2net_wq, &nn->nn_still_up,
				msecs_to_jiffies(o2net_quorum_delay_threshold() *
				O2HB_REGION_TIMEOUT_MS));
	}

	if (was_valid && !valid) {
		if (old_sc) {
			if (old_sc->sc_node->nd_ipnet_type == IPV4_TYPE)
				mlog(ML_NOTICE, "o2net: no longer connected to "
						SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
			else
				mlog(ML_NOTICE, "o2net: no longer connected to "
						SC_IPV6_NODEF_FMT "\n", SC_IPV6_NODEF_ARGS(old_sc));
		}
		o2net_complete_nodes_nsw(nn);
	}

	if (!was_valid && valid) {
		o2quo_conn_up(o2net_num_from_nn(nn));
		cancel_delayed_work(&nn->nn_connect_expired);
		cancel_delayed_work(&nn->nn_still_up);
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: %s " SC_NODEF_FMT "\n",
					o2nm_this_node() > sc->sc_node->nd_num ?
					"connected to" : "accepted connection from",
					SC_NODEF_ARGS(sc));
		else
			mlog(ML_NOTICE, "o2net: %s " SC_IPV6_NODEF_FMT "\n",
					o2nm_this_node() > sc->sc_node->nd_num ?
					"connected to" : "accepted connection from",
					SC_IPV6_NODEF_ARGS(sc));
	}

	/* trigger the connecting worker func as long as we're not valid,
	 * it will back off if it shouldn't connect.  This can be called
	 * from node config teardown and so needs to be careful about
	 * the work queue actually being up. */
	if (!valid && o2net_wq) {
		unsigned long delay;
		/* delay if we're within a RECONNECT_DELAY of the
		 * last attempt */
		delay = (nn->nn_last_connect_attempt +
			 msecs_to_jiffies(o2net_reconnect_delay()))
			- jiffies;
		if (delay > msecs_to_jiffies(o2net_reconnect_delay()))
			delay = 0;
		mlog(ML_CONN, "o2net: queueing conn attempt in %lu jiffies\n", delay);
		queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);

		/*
		 * Delay the expired work after idle timeout.
		 *
		 * We might have lots of failed connection attempts that run
		 * through here but we only cancel the connect_expired work when
		 * a connection attempt succeeds.  So only the first enqueue of
		 * the connect_expired work will do anything.  The rest will see
		 * that it's already queued and do nothing.
		 */
		delay += msecs_to_jiffies(o2net_idle_timeout());
		queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay);
	}

	/* keep track of the nn's sc ref for the caller */
	if ((old_sc == NULL) && sc)
		sc_get(sc);
	if (old_sc && (old_sc != sc)) {
		o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
		sc_put(old_sc);
	}
}

/* see o2net_register_callbacks() */
static void o2net_data_ready(struct sock *sk)
{
	void (*ready)(struct sock *sk);
	struct o2net_sock_container *sc;

	read_lock_bh(&sk->sk_callback_lock);
	sc = sk->sk_user_data;
	if (sc) {
		sclog(sc, "data_ready hit\n");
		o2net_set_data_ready_time(sc);
		o2net_sc_queue_work(sc, &sc->sc_rx_work);
		ready = sc->sc_data_ready;
	} else {
		ready = sk->sk_data_ready;
	}
	read_unlock_bh(&sk->sk_callback_lock);

	ready(sk);
}

/* see o2net_register_callbacks() */
static void o2net_state_change(struct sock *sk)
{
	void (*state_change)(struct sock *sk);
	struct o2net_sock_container *sc;

	read_lock_bh(&sk->sk_callback_lock);
	sc = sk->sk_user_data;
	if (sc == NULL) {
		state_change = sk->sk_state_change;
		goto out;
	}

	sclog(sc, "state_change to %d\n", sk->sk_state);

	state_change = sc->sc_state_change;

	switch(sk->sk_state) {
	/* ignore connecting sockets as they make progress */
	case TCP_SYN_SENT:
	case TCP_SYN_RECV:
		break;
	case TCP_ESTABLISHED:
		o2net_sc_queue_work(sc, &sc->sc_connect_work);
		break;
	default:
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: connection to " SC_NODEF_FMT
			      " shutdown, state %d\n",
			      SC_NODEF_ARGS(sc), sk->sk_state);
		else
			mlog(ML_NOTICE, "o2net: connection to " SC_IPV6_NODEF_FMT
			      " shutdown, state %d\n",
			      SC_IPV6_NODEF_ARGS(sc), sk->sk_state);
		o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
		break;
	}
out:
	read_unlock_bh(&sk->sk_callback_lock);
	state_change(sk);
}

/*
 * we register callbacks so we can queue work on events before calling
 * the original callbacks.  our callbacks our careful to test user_data
 * to discover when they've reaced with o2net_unregister_callbacks().
 */
static void o2net_register_callbacks(struct sock *sk,
				     struct o2net_sock_container *sc)
{
	write_lock_bh(&sk->sk_callback_lock);

	/* accepted sockets inherit the old listen socket data ready */
	if (sk->sk_data_ready == o2net_listen_data_ready) {
		sk->sk_data_ready = sk->sk_user_data;
		sk->sk_user_data = NULL;
	}

	BUG_ON(sk->sk_user_data != NULL);
	sk->sk_user_data = sc;
	sc_get(sc);

	sc->sc_data_ready = sk->sk_data_ready;
	sc->sc_state_change = sk->sk_state_change;
	sk->sk_data_ready = o2net_data_ready;
	sk->sk_state_change = o2net_state_change;

	mutex_init(&sc->sc_send_lock);

	write_unlock_bh(&sk->sk_callback_lock);
}

static int o2net_unregister_callbacks(struct sock *sk,
			           struct o2net_sock_container *sc)
{
	int ret = 0;

	write_lock_bh(&sk->sk_callback_lock);
	if (sk->sk_user_data == sc) {
		ret = 1;
		sk->sk_user_data = NULL;
		sk->sk_data_ready = sc->sc_data_ready;
		sk->sk_state_change = sc->sc_state_change;
	}
	write_unlock_bh(&sk->sk_callback_lock);

	return ret;
}

static void o2net_pre_data_ready(struct sock *sk)
{
	void (*ready)(struct sock *sk);
	struct o2net_sock_container *sc;

	read_lock_bh(&sk->sk_callback_lock);
	sc = sk->sk_user_data;
	if (sc) {
		sclog(sc, "data_ready hit\n");
		o2net_pre_sc_queue_work(sc, &sc->sc_pre_rx_work);
		ready = sc->sc_data_ready;
	} else {
		ready = sk->sk_data_ready;
	}
	read_unlock_bh(&sk->sk_callback_lock);

	ready(sk);
}

static void o2net_pre_state_change(struct sock *sk)
{
	void (*state_change)(struct sock *sk);
	struct o2net_sock_container *sc;

	read_lock_bh(&sk->sk_callback_lock);
	sc = sk->sk_user_data;
	if (sc == NULL) {
		state_change = sk->sk_state_change;
		goto out;
	}

	sclog(sc, "state_change to %d\n", sk->sk_state);

	state_change = sc->sc_state_change;

	switch (sk->sk_state) {
	/* ignore connecting sockets as they make progress */
	case TCP_SYN_SENT:
	case TCP_SYN_RECV:
		break;
	case TCP_ESTABLISHED:
		o2net_pre_sc_queue_work(sc, &sc->sc_pre_connect_work);
		break;
	default:
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: connection to " SC_NODEF_FMT
					" shutdown, state %d\n",
					SC_PRE_NODEF_ARGS(sc), sk->sk_state);
		else
			mlog(ML_NOTICE, "o2net_pre: connection to " SC_IPV6_NODEF_FMT
					" shutdown, state %d\n",
					SC_IPV6_PRE_NODEF_ARGS(sc), sk->sk_state);
		o2net_pre_sc_queue_work(sc, &sc->sc_pre_shutdown_work);
		break;
	}
out:
	read_unlock_bh(&sk->sk_callback_lock);
	state_change(sk);
}

static void o2net_pre_register_callbacks(struct sock *sk,
		struct o2net_sock_container *sc)
{
	write_lock_bh(&sk->sk_callback_lock);

	/* accepted sockets inherit the old listen socket data ready */
	if (sk->sk_data_ready == o2net_pre_listen_data_ready) {
		sk->sk_data_ready = sk->sk_user_data;
		sk->sk_user_data = NULL;
	}

	BUG_ON(sk->sk_user_data != NULL);
	sk->sk_user_data = sc;
	sc_get(sc);

	sc->sc_data_ready = sk->sk_data_ready;
	sc->sc_state_change = sk->sk_state_change;
	sk->sk_data_ready = o2net_pre_data_ready;
	sk->sk_state_change = o2net_pre_state_change;

	mutex_init(&sc->sc_send_lock);

	write_unlock_bh(&sk->sk_callback_lock);
}

static void o2net_pre_ensure_shutdown(struct o2net_node *nn,
		struct o2net_sock_container *sc, int err)
{
	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	if (nn->nn_sc == sc)
		o2net_pre_set_nn_state(nn, NULL, 0, err);
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);
}

void o2net_pre_shutdown_socket(u16 node_num)
{
	struct o2net_node *nn = o2net_pre_nn_from_num(node_num);

	o2net_pre_ensure_shutdown(nn, nn->nn_sc, -ENOTCONN);

	if (o2net_pre_wq) {
		cancel_delayed_work(&nn->nn_pre_connect_expired);
		flush_workqueue(o2net_pre_wq);
	}
}

static void o2net_pre_shutdown_sc(struct work_struct *work)
{
	struct o2net_sock_container *sc =
		container_of(work, struct o2net_sock_container,
				sc_pre_shutdown_work);
	struct o2net_node *nn = o2net_pre_nn_from_num(sc->sc_node->nd_num);

	sclog(sc, "shutting down\n");

	/* drop the callbacks ref and call shutdown only once */
	if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
		/* we shouldn't flush as we're in the thread, the
		 * races with pending sc work structs are harmless */
		del_timer_sync(&sc->sc_idle_timeout);
		sc_put(sc);
		kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
	}

	o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
	sc_put(sc);
}

/*
 * this is a little helper that is called by callers who have seen a problem
 * with an sc and want to detach it from the nn if someone already hasn't beat
 * them to it.  if an error is given then the shutdown will be persistent
 * and pending transmits will be canceled.
 */
static void o2net_ensure_shutdown(struct o2net_node *nn,
			           struct o2net_sock_container *sc,
				   int err)
{
	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	mlog(ML_CONN, "o2net: node %u shutdown, sc %p\n", o2net_num_from_nn(nn), sc);
	if (sc && nn->nn_sc == sc) {
		/* Initialize the nn_timeout so that the next connection attempt
		 * will continue in o2net_start_connect.
		 */
		if (!err)
			atomic_set(&nn->nn_timeout, 1);
		o2net_set_nn_state(nn, NULL, 0, err);
	}
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);
}

/*
 * This work queue function performs the blocking parts of socket shutdown.  A
 * few paths lead here.  set_nn_state will trigger this callback if it sees an
 * sc detached from the nn.  state_change will also trigger this callback
 * directly when it sees errors.  In that case we need to call set_nn_state
 * ourselves as state_change couldn't get the nn_lock and call set_nn_state
 * itself.
 */
static void o2net_shutdown_sc(struct work_struct *work)
{
	struct o2net_sock_container *sc =
		container_of(work, struct o2net_sock_container,
			     sc_shutdown_work);
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);

	sclog(sc, "shutting down\n");

	/* drop the callbacks ref and call shutdown only once */
	if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
		/* we shouldn't flush as we're in the thread, the
		 * races with pending sc work structs are harmless */
		del_timer_sync(&sc->sc_idle_timeout);
		o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
		sc_put(sc);
		kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
	}

	/* not fatal so failed connects before the other guy has our
	 * heartbeat can be retried */
	o2net_ensure_shutdown(nn, sc, 0);
	sc_put(sc);
}

/* ------------------------------------------------------------ */

static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
			     u32 key)
{
	int ret = memcmp(&nmh->nh_key, &key, sizeof(key));

	if (ret == 0)
		ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));

	return ret;
}

static struct o2net_msg_handler *
o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
			  struct rb_node **ret_parent)
{
	struct rb_node **p = &o2net_handler_tree.rb_node;
	struct rb_node *parent = NULL;
	struct o2net_msg_handler *nmh, *ret = NULL;
	int cmp;

	while (*p) {
		parent = *p;
		nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
		cmp = o2net_handler_cmp(nmh, msg_type, key);

		if (cmp < 0)
			p = &(*p)->rb_left;
		else if (cmp > 0)
			p = &(*p)->rb_right;
		else {
			ret = nmh;
			break;
		}
	}

	if (ret_p != NULL)
		*ret_p = p;
	if (ret_parent != NULL)
		*ret_parent = parent;

	return ret;
}

static void o2net_handler_kref_release(struct kref *kref)
{
	struct o2net_msg_handler *nmh;
	nmh = container_of(kref, struct o2net_msg_handler, nh_kref);

	kfree(nmh);
}

static void o2net_handler_put(struct o2net_msg_handler *nmh)
{
	kref_put(&nmh->nh_kref, o2net_handler_kref_release);
}

/* max_len is protection for the handler func.  incoming messages won't
 * be given to the handler if their payload is longer than the max. */
int o2net_register_handler(u32 msg_type, u32 key, u32 max_len, u32 min_len,
			   o2net_msg_handler_func *func, void *data,
			   o2net_post_msg_handler_func *post_func,
			   struct list_head *unreg_list)
{
	struct o2net_msg_handler *nmh = NULL;
	struct rb_node **p, *parent;
	int ret = 0;

	if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
		mlog(0, "o2net: max_len for message handler out of range: %u\n",
			max_len);
		ret = -EINVAL;
		goto out;
	}

	if (!msg_type) {
		mlog(0, "o2net: no message type provided: %u, %p\n", msg_type, func);
		ret = -EINVAL;
		goto out;

	}
	if (!func) {
		mlog(0, "o2net: no message handler provided: %u, %p\n",
		       msg_type, func);
		ret = -EINVAL;
		goto out;
	}

       	nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS);
	if (nmh == NULL) {
		ret = -ENOMEM;
		goto out;
	}

	nmh->nh_func = func;
	nmh->nh_func_data = data;
	nmh->nh_post_func = post_func;
	nmh->nh_msg_type = msg_type;
	nmh->nh_max_len = max_len;
	nmh->nh_min_len = min_len;
	nmh->nh_key = key;
	/* the tree and list get this ref.. they're both removed in
	 * unregister when this ref is dropped */
	kref_init(&nmh->nh_kref);
	INIT_LIST_HEAD(&nmh->nh_unregister_item);

	write_lock(&o2net_handler_lock);
	if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
		ret = -EEXIST;
	else {
		rb_link_node(&nmh->nh_node, parent, p);
		rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
		list_add_tail(&nmh->nh_unregister_item, unreg_list);

		mlog(ML_TCP, "o2net: registered handler func %p type %u key %08x\n",
		     func, msg_type, key);
		/* we've had some trouble with handlers seemingly vanishing. */
		mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
					&parent) == NULL,
					"couldn't find handler we *just* registered for type %u key %08x\n",
					msg_type, key);
	}
	write_unlock(&o2net_handler_lock);

out:
	if (ret)
		kfree(nmh);

	return ret;
}
EXPORT_SYMBOL_GPL(o2net_register_handler);

void o2net_unregister_handler_list(struct list_head *list)
{
	struct o2net_msg_handler *nmh, *n;

	write_lock(&o2net_handler_lock);
	list_for_each_entry_safe(nmh, n, list, nh_unregister_item) {
		mlog(ML_TCP, "o2net: unregistering handler func %p type %u key %08x\n",
		     nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
		rb_erase(&nmh->nh_node, &o2net_handler_tree);
		list_del_init(&nmh->nh_unregister_item);
		kref_put(&nmh->nh_kref, o2net_handler_kref_release);
	}
	write_unlock(&o2net_handler_lock);
}
EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);

static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
{
	struct o2net_msg_handler *nmh;

	read_lock(&o2net_handler_lock);
	nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
	if (nmh)
		kref_get(&nmh->nh_kref);
	read_unlock(&o2net_handler_lock);

	return nmh;
}

/* ------------------------------------------------------------ */

static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
{
	struct kvec vec = { .iov_len = len, .iov_base = data, };
	struct msghdr msg = { .msg_flags = MSG_DONTWAIT, };
	iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, len);
	return sock_recvmsg(sock, &msg, MSG_DONTWAIT);
}

static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
			      size_t veclen, size_t total)
{
	int ret;
	struct msghdr msg = {.msg_flags = 0,};

	if (sock == NULL) {
		ret = -EINVAL;
		goto out;
	}

	ret = kernel_sendmsg(sock, &msg, vec, veclen, total);
	if (likely(ret == total))
		return 0;
	mlog(ML_ERROR, "o2net: sendmsg returned %d instead of %zu\n", ret, total);
	if (ret >= 0)
		ret = -EPIPE; /* should be smarter, I bet */
out:
	mlog(0, "o2net: returning error: %d\n", ret);
	return ret;
}

static void o2net_sendpage(struct o2net_sock_container *sc,
			   void *kmalloced_virt,
			   size_t size)
{
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
	ssize_t ret;

	while (1) {
		mutex_lock(&sc->sc_send_lock);
		ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
						 virt_to_page(kmalloced_virt),
						 offset_in_page(kmalloced_virt),
						 size, MSG_DONTWAIT);
		mutex_unlock(&sc->sc_send_lock);
		if (ret == size)
			break;
		if (ret == (ssize_t)-EAGAIN) {
			if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
				mlog(0, "o2net: sendpage of size %zu to " SC_NODEF_FMT
				     " returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
			else
				mlog(0, "o2net: sendpage of size %zu to " SC_IPV6_NODEF_FMT
				     " returned EAGAIN\n", size, SC_IPV6_NODEF_ARGS(sc));
			cond_resched();
			continue;
		}
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_ERROR, "o2net: sendpage of size %zu to " SC_NODEF_FMT
			     " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
		else
			mlog(ML_ERROR, "o2net: sendpage of size %zu to " SC_IPV6_NODEF_FMT
			     " failed with %zd\n", size, SC_IPV6_NODEF_ARGS(sc), ret);
		o2net_ensure_shutdown(nn, sc, 0);
		break;
	}
}

static void o2net_pre_sendpage(struct o2net_sock_container *sc,
		const void *kmalloced_virt, size_t size)
{
	struct o2net_node *nn = o2net_pre_nn_from_num(sc->sc_node->nd_num);
	ssize_t ret;

	while (1) {
		mutex_lock(&sc->sc_send_lock);
		ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
				virt_to_page(kmalloced_virt),
				offset_in_page(kmalloced_virt),
				size, MSG_DONTWAIT);
		mutex_unlock(&sc->sc_send_lock);
		if (ret == size)
			break;
		if (ret == (ssize_t)-EAGAIN) {
			if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
				mlog(0, "o2net_pre: sendpage of size %zu to " SC_NODEF_FMT
						" returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
			else
				mlog(0, "o2net_pre: sendpage of size %zu to " SC_IPV6_NODEF_FMT
						" returned EAGAIN\n", size, SC_IPV6_NODEF_ARGS(sc));
			cond_resched();
			continue;
		}
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_ERROR, "o2net_pre: sendpage of size %zu to " SC_NODEF_FMT
				     " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
		else
			mlog(ML_ERROR, "o2net_pre: sendpage of size %zu to " SC_IPV6_NODEF_FMT
				     " failed with %zd\n", size, SC_IPV6_NODEF_ARGS(sc), ret);
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
		break;
	}
}

static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
{
	memset(msg, 0, sizeof(struct o2net_msg));
	msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
	msg->data_len = cpu_to_be16(data_len);
	msg->msg_type = cpu_to_be16(msg_type);
	msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
	msg->status = 0;
	msg->key = cpu_to_be32(key);
	msg->ack_num = 0;
	msg->seq_num = 0;
}

static int o2net_tx_can_proceed(struct o2net_node *nn,
			        struct o2net_sock_container **sc_ret,
				int *error)
{
	int ret = 0;

	spin_lock(&nn->nn_lock);
	if (nn->nn_persistent_error) {
		ret = 1;
		*sc_ret = NULL;
		*error = nn->nn_persistent_error;
	} else if (nn->nn_sc_valid) {
		kref_get(&nn->nn_sc->sc_kref);

		ret = 1;
		*sc_ret = nn->nn_sc;
		*error = 0;
	}
	spin_unlock(&nn->nn_lock);

	return ret;
}

/* Get a map of all nodes to which this node is currently connected to */
void o2net_fill_node_map(unsigned long *map, unsigned bytes)
{
	struct o2net_sock_container *sc;
	int node, ret;

	BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));

	memset(map, 0, bytes);
	for (node = 0; node < O2NM_MAX_NODES; ++node) {
		if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret))
			continue;
		if (!ret) {
			set_bit(node, map);
			sc_put(sc);
		}
	}
}
EXPORT_SYMBOL_GPL(o2net_fill_node_map);

static int o2net_pre_send_message_vec_v2(struct o2net_sock_container *sc,
		struct kvec *caller_vec, size_t caller_veclen, size_t caller_bytes,
		u16 target_node, int *status)
{
	int ret = 0, i;
	u16 crc = 0;
	struct o2net_msg *msg = NULL;
	size_t veclen;
	struct kvec *vec = NULL;
	struct o2net_node *nn = o2net_pre_nn_from_num(target_node);
	struct o2net_status_wait nsw = {
		.ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
	};

	veclen = caller_veclen + 1;
	vec = kmalloc_array(veclen, sizeof(struct kvec), GFP_ATOMIC);
	if (vec == NULL) {
		mlog(0, "o2net_pre: failed to %zu element kvec!\n", veclen);
		ret = -ENOMEM;
		goto out;
	}

	msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
	if (!msg) {
		mlog(0, "o2net_pre: failed to allocate a o2net_msg!\n");
		ret = -ENOMEM;
		goto out;
	}

	o2net_init_msg(msg, caller_bytes, 0, 0);

	vec[0].iov_len = sizeof(struct o2net_msg);
	vec[0].iov_base = msg;
	memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));

	mutex_lock(&nn->nn_mutex);
	ret = o2net_prep_nsw(nn, &nsw);
	mutex_unlock(&nn->nn_mutex);
	if (ret)
		goto out;

	/* calc crc16 for message */
	for (i = 0; i < veclen; i++)
		crc = crc16(crc, vec[i].iov_base, vec[i].iov_len);
	msg->crc = cpu_to_be16(crc);
	nsw.crc = crc;

	/* finally, convert the message header to network byte-order
	 * and send */
	mutex_lock(&sc->sc_send_lock);
	ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
			sizeof(struct o2net_msg) + caller_bytes);
	mutex_unlock(&sc->sc_send_lock);
	msglog_level(ML_MSG, msg, "sending returned %d\n", ret);

	if (ret < 0) {
		mlog(ML_ERROR, "o2net_pre: error returned from o2net_send_tcp_msg=%d\n", ret);
		goto out;
	}

	wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));

	/* Note that we avoid overwriting the callers status return
	 * variable if a system error was reported on the other
	 * side. Callers beware. */
	ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
	if (status && !ret)
		*status = nsw.ns_status;

	mlog(0, "o2net_pre: woken, returning system status %d, user status %d\n",
			ret, nsw.ns_status);

out:
	kfree(vec);
	kfree(msg);

	o2net_complete_nsw(nn, &nsw, 0, 0, 0);
	return ret;
}

static int o2net_pre_send_message_vec(struct kvec *caller_vec,
		size_t caller_veclen, u16 target_node, int *status)
{
	int ret = 0;
	size_t caller_bytes = 0;
	struct o2net_sock_container *sc = NULL;
	struct o2net_node *nn = o2net_pre_nn_from_num(target_node);

	if (o2net_pre_wq == NULL) {
		mlog(0, "o2net_pre: attempt to tx without o2netd running\n");
		ret = -ESRCH;
		goto out;
	}

	caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
	if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
		mlog(0, "o2net_pre: total payload len %zu too large\n", caller_bytes);
		ret = -EINVAL;
		goto out;
	}

	if (target_node == o2nm_this_node()) {
		ret = -ELOOP;
		goto out;
	}

	/* change o2net_pre to the way o2net behaves, only send messages
	 * after connection is set up */
	wait_event_timeout(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret),
			msecs_to_jiffies(o2net_idle_timeout()));
	if (ret)
		goto out;

	if (nn->o2net_msg_version == O2NET_MSG) {
		ret = o2net_pre_send_message_vec_v2(sc, caller_vec, caller_veclen,
			caller_bytes, target_node, status);
	} else {
		mlog(ML_ERROR, "o2net_pre: invalid msg version: %d\n", nn->o2net_msg_version);
		ret = -EINVAL;
	}

out:
	if (sc)
		sc_put(sc);

	return ret;
}

int o2net_pre_send_message(void *data, u32 len,
		u16 target_node, int *status)
{
	struct kvec vec = {
		.iov_base = data,
		.iov_len = len,
	};
	return o2net_pre_send_message_vec(&vec, 1,
			target_node, status);
}

int o2net_send_message_vec_v2(struct o2net_sock_container *sc,
			   u32 msg_type, u32 key, struct kvec *caller_vec,
			   size_t caller_veclen, size_t caller_bytes, u16 target_node,
			   u64 seq_num, int *status)
{
	int ret = 0, i;
	u16 crc = 0;
	u64 ack_num = 0;
	size_t veclen;
	struct o2net_msg *msg = NULL;
	struct kvec *vec = NULL;
	struct o2net_node *nn = o2net_nn_from_num(target_node);
	struct o2net_status_wait *nsw = NULL;
	struct o2net_send_tracking *nst = NULL;

	nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_ATOMIC);
	if (nst == NULL) {
		mlog(0, "o2net: failed to allocate a o2net_send_tracking!!\n");
		ret = -ENOMEM;
		goto out;
	}

	o2net_init_nst(nst, msg_type, key, current, target_node);
	mlog(ML_TCP, "o2net: seq_num %llu, key 0x%x, msg_type %d, target_node %d\n",
		seq_num, key, msg_type, target_node);

	o2net_debug_add_nst(nst);
	o2net_set_nst_sock_time(nst);
	o2net_set_nst_sock_container(nst, sc);

	veclen = caller_veclen + 1;
	vec = kmalloc_array(veclen, sizeof(struct kvec), GFP_ATOMIC);
	if (vec == NULL) {
		mlog(0, "o2net: failed to %zu element kvec!\n", veclen);
		ret = -ENOMEM;
		goto out;
	}

	msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
	if (!msg) {
		mlog(0, "o2net: failed to allocate a o2net_msg!\n");
		ret = -ENOMEM;
		goto out;
	}

	nsw = kmalloc(sizeof(struct o2net_status_wait), GFP_ATOMIC);
	if (!nsw) {
		mlog(0, "o2net: failed to allocate a o2net_status_wait!\n");
		ret = -ENOMEM;
		goto out;
	}
	INIT_LIST_HEAD(&nsw->ns_node_item);

	o2net_init_msg(msg, caller_bytes, msg_type, key);

	msg->seq_num = cpu_to_be64(seq_num);
	spin_lock(&nn->nn_lock);
	ack_num = o2net_get_ack_num(nn, seq_num, key);
	spin_unlock(&nn->nn_lock);
	mlog(ML_TCP, "o2net: ack_num %llu\n", ack_num);

	msg->ack_num = cpu_to_be64(ack_num);

	vec[0].iov_len = sizeof(struct o2net_msg);
	vec[0].iov_base = msg;
	memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));

	mutex_lock(&nn->nn_mutex);
	ret = o2net_prep_nsw(nn, nsw);
	mutex_unlock(&nn->nn_mutex);
	if (ret)
		goto out;

	msg->msg_num = cpu_to_be32(nsw->ns_id);
	o2net_set_nst_msg_id(nst, nsw->ns_id);

	o2net_set_nst_send_time(nst);

	/* calc crc16 for message */
	for (i = 0; i < veclen; i++)
		crc = crc16(crc, vec[i].iov_base, vec[i].iov_len);
	msg->crc = cpu_to_be16(crc);
	nsw->crc = crc;

	/* finally, convert the message header to network byte-order
	 * and send */
	mutex_lock(&sc->sc_send_lock);
	ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
				 sizeof(struct o2net_msg) + caller_bytes);
	mutex_unlock(&sc->sc_send_lock);
	msglog_level(ML_MSG, msg, "sending returned %d\n", ret);
	if (ret < 0) {
		mlog(0, "o2net: error returned from o2net_send_tcp_msg=%d\n", ret);
		goto out;
	}

	/* wait on other node's handler */
	o2net_set_nst_status_time(nst);
	wait_event(nsw->ns_wq, o2net_nsw_completed(nn, nsw));

	o2net_update_send_stats(nst, sc);

	/* Note that we avoid overwriting the callers status return
	 * variable if a system error was reported on the other
	 * side. Callers beware. */
	ret = o2net_sys_err_to_errno(nsw->ns_sys_status);
	if (status && !ret)
		*status = nsw->ns_status;

	mlog(0, "o2net: woken, returning system status %d, user status %d\n",
	     ret, nsw->ns_status);
out:
	if (nst)
		o2net_debug_del_nst(nst); /* must be before dropping sc and node */

	kfree(vec);
	kfree(msg);

	if (nsw)
		o2net_complete_nsw(nn, nsw, 0, 0, 0);
	kfree(nsw);
	kfree(nst);

	return ret;
}

int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
			size_t caller_veclen, u16 target_node, u64 seq_num, int *status)
{
	int ret = 0;
	size_t caller_bytes = 0;
	struct o2net_sock_container *sc = NULL;
	struct o2net_node *nn = o2net_nn_from_num(target_node);

	if (o2net_wq == NULL) {
		mlog(0, "o2net: attempt to tx without o2netd running\n");
		ret = -ESRCH;
		goto out;
	}

	if (caller_veclen == 0) {
		mlog(0, "o2net: bad kvec array length\n");
		ret = -EINVAL;
		goto out;
	}

	caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
	if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
		mlog(0, "o2net: total payload len %zu too large\n", caller_bytes);
		ret = -EINVAL;
		goto out;
	}

	if (target_node == o2nm_this_node()) {
		ret = -ELOOP;
		goto out;
	}

	ret = -ENOTCONN;
	wait_event_timeout(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret),
			msecs_to_jiffies(o2net_idle_timeout()));
	if (ret)
		goto out;

	if (nn->o2net_msg_version == O2NET_MSG) {
		ret = o2net_send_message_vec_v2(sc, msg_type, key, caller_vec, caller_veclen,
			caller_bytes, target_node, seq_num, status);
	} else {
		mlog(ML_ERROR, "o2net: invalid msg version: %d\n", nn->o2net_msg_version);
		ret = -EINVAL;
	}

out:
	if (sc)
		sc_put(sc);
	return ret;
}

EXPORT_SYMBOL_GPL(o2net_send_message_vec);

int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
		       u16 target_node, u64 seq_num, int *status)
{
	struct kvec vec = {
		.iov_base = data,
		.iov_len = len,
	};
	return o2net_send_message_vec(msg_type, key, &vec, 1,
				      target_node, seq_num, status);
}
EXPORT_SYMBOL_GPL(o2net_send_message);

int o2net_send_message_vec_authorized_sequence(u32 msg_type, u32 key,
		struct kvec *vec, size_t veclen, u16 target_node, int authorized,
		u64 seq_num, int *status)
{
	if (!authorized)
		return -ENOPROTOOPT;

	return o2net_send_message_vec(msg_type, key, vec, veclen,
			target_node, seq_num, status);
}
EXPORT_SYMBOL_GPL(o2net_send_message_vec_authorized_sequence);

int o2net_send_message_authorized_sequence(u32 msg_type, u32 key,
		void *data, u32 len, u16 target_node, int authorized,
		u64 seq_num, int *status)
{
	struct kvec vec = {
		.iov_base = data,
		.iov_len = len,
	};
	return o2net_send_message_vec_authorized_sequence(msg_type, key, &vec, 1,
			target_node, authorized, seq_num, status);
}
EXPORT_SYMBOL_GPL(o2net_send_message_authorized_sequence);

static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
				   enum o2net_system_error syserr, int err)
{
	u16 crc;
	struct kvec vec = {
		.iov_base = hdr,
		.iov_len = sizeof(struct o2net_msg),
	};

	BUG_ON(syserr >= O2NET_ERR_MAX);

	/* leave other fields intact from the incoming message, msg_num
	 * in particular */
	hdr->sys_status = cpu_to_be32(syserr);
	hdr->status = cpu_to_be32(err);
	hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC);  // twiddle the magic
	hdr->data_len = 0;
	hdr->crc = 0;

	/* calc crc16 for message */
	crc = crc16(0, vec.iov_base, vec.iov_len);
	hdr->crc = cpu_to_be16(crc);

	msglog_level(ML_MSG, hdr, "about to send status magic %d\n", err);
	/* hdr has been in host byteorder this whole time */
	return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
}

static unsigned int find_domain_index(struct o2net_seq_status_domain **msg_list,
		u32 key, int *domain_count)
{
	unsigned int i;

	for (i = 0; i < O2NET_MAX_DOMAIN_NUMBER; i++) {
		if (msg_list[i]->domain_key == 0) {
			msg_list[i]->domain_key = key;
			(*domain_count)++;
			break;
		}
		if (msg_list[i]->domain_key == key)
			break;
	}
	return i;
}

static int is_key_invalid(struct o2net_seq_status_domain **msg_list,
		u64 seq_num, u32 key)
{
	if (msg_list == NULL || seq_num == 0 || key == 0)
		return 1;

	return 0;
}

static u64 o2net_get_ack_num(struct o2net_node *nn, u64 seq_num, u32 key)
{
	int index;
	struct o2net_ack_num *ack_num_list;
	struct o2net_ack_num *tmp;
	u64 ack_num = 0;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;

	assert_spin_locked(&nn->nn_lock);

	if (is_key_invalid(msg_list, seq_num, key))
		return 0;

	index = find_domain_index(msg_list, key, &nn->nn_domain_count);
	if (index == O2NET_MAX_DOMAIN_NUMBER)
		return 0;

	list_for_each_entry_safe(ack_num_list, tmp,
			&msg_list[index]->o2net_ack_num_list,
			o2net_ack_num_item) {
		if (ack_num_list->ack_num) {
			ack_num = ack_num_list->ack_num;
			list_del_init(&ack_num_list->o2net_ack_num_item);
			kmem_cache_free(o2net_ack_num_cache, ack_num_list);
			break;
		}
	}
	return ack_num;
}
static void o2net_set_ack_num(struct o2net_node *nn, u64 seq_num, u32 key)
{
	int index;
	struct o2net_ack_num *ack_num_list;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;

	if (is_key_invalid(msg_list, seq_num, key))
		return;

	ack_num_list = kmem_cache_alloc(o2net_ack_num_cache, GFP_NOFS);
	if (!ack_num_list) {
		mlog(ML_ERROR, "o2net: cache memory not enough\n");
		return;
	}
	spin_lock(&nn->nn_lock);
	index = find_domain_index(msg_list, key, &nn->nn_domain_count);
	if (index == O2NET_MAX_DOMAIN_NUMBER) {
		spin_unlock(&nn->nn_lock);
		kmem_cache_free(o2net_ack_num_cache, ack_num_list);
		return;
	}

	ack_num_list->ack_num = seq_num;
	list_add_tail(&ack_num_list->o2net_ack_num_item,
			&msg_list[index]->o2net_ack_num_list);
	spin_unlock(&nn->nn_lock);
}

static void o2net_add_seqnum_status(struct o2net_node *nn,
		u64 seq_num, u32 key, int status)
{
	struct o2net_seq_status *seq_status;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;
	int index;

	if (is_key_invalid(msg_list, seq_num, key))
		return;

	seq_status = kmem_cache_alloc(o2net_msg_cache, GFP_NOFS);
	if (!seq_status) {
		mlog(ML_ERROR, "o2net: cache memory not enough\n");
		return;
	}

	spin_lock(&nn->nn_lock);
	index = find_domain_index(msg_list, key, &nn->nn_domain_count);
	if (index == O2NET_MAX_DOMAIN_NUMBER) {
		spin_unlock(&nn->nn_lock);
		kmem_cache_free(o2net_msg_cache, seq_status);
		return;
	}
	seq_status->seq_num = seq_num;
	seq_status->status = status;
	seq_status->time_start = jiffies;

	mlog(ML_TCP,
		"o2net: add msg from node %u: seq_num %llu, key 0x%x, index %d, seq_status %p\n",
		o2net_num_from_nn(nn), seq_num, key, index, seq_status);

	list_add_tail(&seq_status->o2net_seq_status_item,
			&msg_list[index]->o2net_seq_status_list);

	spin_unlock(&nn->nn_lock);
}

static void o2net_delete_seqnum_status(struct o2net_node *nn,
		u64 seq_num, u32 key)
{
	int index;
	struct o2net_seq_status *seq_status = NULL;
	struct o2net_seq_status *tmp = NULL;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;

	assert_spin_locked(&nn->nn_lock);

	if (is_key_invalid(msg_list, seq_num, key))
		return;

	index = find_domain_index(msg_list, key, &nn->nn_domain_count);
	if (index == O2NET_MAX_DOMAIN_NUMBER)
		return;

	list_for_each_entry_safe(seq_status, tmp,
			&msg_list[index]->o2net_seq_status_list,
			o2net_seq_status_item) {
		if (seq_status->seq_num == seq_num) {
			mlog(ML_TCP,
				"o2net: delete msg from node %u: seq_num %llu, key 0x%x, seq_status %p\n",
				o2net_num_from_nn(nn), seq_num, key, seq_status);
			list_del_init(&seq_status->o2net_seq_status_item);
			kmem_cache_free(o2net_msg_cache, seq_status);
			break;
		}
	}
}

static int o2net_check_seqnum_status(struct o2net_node *nn,
		u64 seq_num, u32 key, int *handler_status)
{
	int index;
	int found = 0;
	struct o2net_seq_status *seq_status;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;

	assert_spin_locked(&nn->nn_lock);

	if (is_key_invalid(msg_list, seq_num, key))
		return found;

	index = find_domain_index(msg_list, key, &nn->nn_domain_count);
	if (index == O2NET_MAX_DOMAIN_NUMBER)
		goto out;

	list_for_each_entry(seq_status, &msg_list[index]->o2net_seq_status_list,
			o2net_seq_status_item) {
		if (seq_status->seq_num == seq_num) {
			*handler_status = seq_status->status;
			found = 1;
			mlog(ML_NOTICE,
				"o2net: return msg from node %u: seq_num %llu, status %d, key 0x%x\n",
				o2net_num_from_nn(nn),
				seq_num, seq_status->status, key);
			break;
		}
	}

out:
	return found;
}

static void o2net_delete_msg_list(struct o2net_seq_status_domain *msg_list)
{
	struct o2net_seq_status *seq_status = NULL;
	struct o2net_seq_status *seq_status_tmp = NULL;
	struct o2net_ack_num *ack_num_list = NULL;
	struct o2net_ack_num *ack_num_list_tmp = NULL;

	msg_list->domain_key = 0;

	list_for_each_entry_safe(seq_status, seq_status_tmp,
			&msg_list->o2net_seq_status_list,
			o2net_seq_status_item) {
		list_del_init(&seq_status->o2net_seq_status_item);
		kmem_cache_free(o2net_msg_cache, seq_status);
	}

	list_for_each_entry_safe(ack_num_list, ack_num_list_tmp,
			&msg_list->o2net_ack_num_list,
			o2net_ack_num_item) {
		list_del_init(&ack_num_list->o2net_ack_num_item);
		kmem_cache_free(o2net_ack_num_cache, ack_num_list);
	}
}

static void o2net_delete_all_seqnum_status(struct o2net_node *nn)
{
	int i;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;

	if (!msg_list)
		return;

	mlog(ML_NOTICE, "o2net: node %u is down, so delete all the seq_num_status\n",
		o2net_num_from_nn(nn));

	nn->nn_domain_count = 0;

	for (i = 0; i < O2NET_MAX_DOMAIN_NUMBER; i++) {
		if (msg_list[i]->domain_key == 0)
			break;

		o2net_delete_msg_list(msg_list[i]);
	}
}

static void o2net_move_seqnum_status(struct o2net_seq_status_domain *new_msg_list,
		struct o2net_seq_status_domain *old_msg_list)
{
	if (old_msg_list->domain_key == 0 || new_msg_list->domain_key != 0) {
		mlog(ML_ERROR,
			"o2net: old msg list is NULL or new msg list has data, this may have error\n");
		return;
	}

	new_msg_list->domain_key = old_msg_list->domain_key;
	old_msg_list->domain_key = 0;

	list_splice_init(&old_msg_list->o2net_seq_status_list,
			&new_msg_list->o2net_seq_status_list);
	list_splice_init(&old_msg_list->o2net_ack_num_list,
			&new_msg_list->o2net_ack_num_list);
}

void o2net_delete_node_seqnum_status(u16 node_num, u32 domain_key)
{
	struct o2net_node *nn = o2net_nn_from_num(node_num);
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;
	int i;

	spin_lock(&nn->nn_lock);
	if (!msg_list || (nn->nn_domain_count == 0))
		goto out;

	mlog(ML_NOTICE, "o2net: node %u exit domain %u, so delete the seq_num_status\n",
			node_num, domain_key);

	for (i = 0; i < O2NET_MAX_DOMAIN_NUMBER; i++) {
		if (msg_list[i]->domain_key == 0)
			break;

		if (msg_list[i]->domain_key != domain_key)
			continue;

		o2net_delete_msg_list(msg_list[i]);

		if ((nn->nn_domain_count - 1) > i)
			o2net_move_seqnum_status(msg_list[i],
					msg_list[nn->nn_domain_count - 1]);

		nn->nn_domain_count--;
		break;
	}

out:
	spin_unlock(&nn->nn_lock);
}
EXPORT_SYMBOL_GPL(o2net_delete_node_seqnum_status);

void o2net_delete_domain_seqnum_status(u32 domain_key)
{
	int i;

	mlog(ML_NOTICE,
		"o2net: dlm %u shutdown, we will delete all seq_num_status in domain\n",
		domain_key);

	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++)
		o2net_delete_node_seqnum_status(i, domain_key);
}
EXPORT_SYMBOL_GPL(o2net_delete_domain_seqnum_status);

/* check crc16 for message, this returns 0 if check successfully */
static int o2net_check_crc16(struct o2net_node *nn,
		struct o2net_msg *msg, u16 nd_num)
{
	u16 crc, orig_crc = be16_to_cpu(msg->crc);
	u16 magic = be16_to_cpu(msg->magic);
	u32 id = be32_to_cpu(msg->msg_num);
	struct o2net_status_wait *nsw;
	int ret = 0;

	/* To support upgrade situation, we need check if
	 * dest node supporting crc. none crc node will send
	 * either zero or the crc we calc before in o2net_msg,
	 * so we can identify none crc node by the crc contains
	 * in msg. */
	if (!orig_crc) {
		msglog_level(ML_MSG, msg,
			"got none crc msg from node[%u] returned %d\n", nd_num, ret);
		goto out;
	}

	if (magic == O2NET_MSG_STATUS_MAGIC) {
		spin_lock(&nn->nn_lock);
		nsw = idr_find(&nn->nn_status_idr, id);
		if (nsw == NULL) {
			ret = -ENOENT;
			mlog(ML_ERROR,
				"o2net: can't find entry according to msg id[%u] from node[%u] returned %d\n",
				id, nd_num, ret);
			spin_unlock(&nn->nn_lock);
			goto out;
		}

		if (nsw->crc == orig_crc) {
			msglog_level(ML_MSG, msg,
				"got none crc msg from node[%u] returned %d\n",
				nd_num, ret);
			spin_unlock(&nn->nn_lock);
			goto out;
		}
		spin_unlock(&nn->nn_lock);
	}

	/* check crc16 for message */
	msg->crc = 0;
	crc = crc16(0, (u8 *)msg, sizeof(struct o2net_msg) +
			be16_to_cpu(msg->data_len));
	if (orig_crc != crc) {
		ret = -EINVAL;
		msglog_level(ML_ERROR, msg,
			"o2net: crc check fail, crc[%u] is not equal to orig_crc[%u] returned %d\n",
			crc, orig_crc, ret);
	}
	msg->crc = cpu_to_be16(orig_crc);

out:
	return ret;
}

/* this returns -errno if the header was unknown or too large, etc.
 * after this is called the buffer us reused for the next message */
static int o2net_process_message(struct o2net_sock_container *sc,
				 struct o2net_msg *hdr)
{
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
	int ret = 0, handler_status = 0;
	enum  o2net_system_error syserr;
	struct o2net_msg_handler *nmh = NULL;
	void *ret_data = NULL;

	msglog_level(ML_MSG, hdr, "processing message\n");

	o2net_sc_postpone_idle(sc);

	switch (be16_to_cpu(hdr->magic)) {
	case O2NET_MSG_STATUS_MAGIC:
		ret = o2net_check_crc16(nn, hdr, sc->sc_node->nd_num);
		if (ret) {
			msglog_level(ML_ERROR, hdr,
				"bad crc from node[%u], returned %d\n",
				sc->sc_node->nd_num, ret);
			goto out;
		}

		/* special type for returning message status */
		if (!o2net_complete_nsw(nn, NULL,
				be32_to_cpu(hdr->msg_num),
				be32_to_cpu(hdr->sys_status),
				be32_to_cpu(hdr->status)))
			o2net_set_ack_num(nn, be64_to_cpu(hdr->seq_num),
					be32_to_cpu(hdr->key));
		goto out;
	case O2NET_MSG_KEEP_REQ_MAGIC:
		o2net_sendpage(sc, o2net_keep_resp,
			       sizeof(*o2net_keep_resp));
		goto out;
	case O2NET_MSG_KEEP_RESP_MAGIC:
		goto out;
	case O2NET_MSG_MAGIC:
		ret = o2net_check_crc16(nn, hdr, sc->sc_node->nd_num);
		if (ret) {
			msglog_level(ML_ERROR, hdr,
				"bad crc from node[%u], returned %d\n",
				sc->sc_node->nd_num, ret);
			goto out;
		}
		break;
	default:
		ret = -EINVAL;
		msglog_level(ML_ERROR, hdr,
			"bad magic from node[%u] returned %d\n",
			sc->sc_node->nd_num, ret);
		goto out;
	}

	/* After unregister handler, ensure seqnum status can not add */
	spin_lock(&nn->nn_lock);
	/* find a handler for it */
	nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
				be32_to_cpu(hdr->key));
	if (!nmh) {
		mlog(ML_TCP, "o2net: couldn't find handler for type %u key %08x\n",
		     be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
		syserr = O2NET_ERR_NO_HNDLR;
		spin_unlock(&nn->nn_lock);
		goto out_respond;
	}

	syserr = O2NET_ERR_NONE;

	/*whether the same msg*/
	o2net_delete_seqnum_status(nn, be64_to_cpu(hdr->ack_num),
			be32_to_cpu(hdr->key));

	ret = o2net_check_seqnum_status(nn, be64_to_cpu(hdr->seq_num),
			be32_to_cpu(hdr->key), &handler_status);
	spin_unlock(&nn->nn_lock);
	if (ret == 1) {
		mlog(ML_ERROR,
				"o2net: It is the same message:type %u, key 0x%x, seq_num %llu, handler_status %d from num %d\n",
				be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key),
				be64_to_cpu(hdr->seq_num), handler_status, sc->sc_node->nd_num);
		goto out_respond;
	}

	if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
		syserr = O2NET_ERR_OVERFLOW;
	if (be16_to_cpu(hdr->data_len) < nmh->nh_min_len)
		syserr = O2NET_ERR_LIMITATION;

	if (syserr != O2NET_ERR_NONE)
		goto out_respond;

	o2net_set_func_start_time(sc);
	sc->sc_msg_key = be32_to_cpu(hdr->key);
	sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
	handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
					     be16_to_cpu(hdr->data_len),
					nmh->nh_func_data, &ret_data);
	o2net_set_func_stop_time(sc);

	o2net_update_recv_stats(sc);

	if (o2hb_test_domain_map_by_key(sc->sc_node->nd_num,
			be32_to_cpu(hdr->key))) {
		o2net_add_seqnum_status(nn, be64_to_cpu(hdr->seq_num),
				be32_to_cpu(hdr->key), handler_status);
	}

out_respond:
	/* this destroys the hdr, so don't use it after this */
	mutex_lock(&sc->sc_send_lock);
	ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
				      handler_status);
	mutex_unlock(&sc->sc_send_lock);
	hdr = NULL;
	mlog(0, "o2net: sending handler status %d, syserr %d returned %d\n",
	     handler_status, syserr, ret);

	if (nmh) {
		BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL);
		if (nmh->nh_post_func)
			(nmh->nh_post_func)(handler_status, nmh->nh_func_data,
					    ret_data);
	}

out:
	if (nmh)
		o2net_handler_put(nmh);
	return ret;
}

static int hmac_sha256(const u8 *key, u8 ksize, const u8 *plaintext, u8 psize, u8 *output)
{
	int ret;
	struct crypto_shash *tfm;
	struct shash_desc *shash;

	if (!ksize)
		return -EINVAL;

	tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
	if (IS_ERR_OR_NULL(tfm)) {
		mlog(ML_ERROR, "o2net: crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
		return PTR_ERR(tfm);
	}

	ret = crypto_shash_setkey(tfm, key, ksize);
	if (ret) {
		mlog(ML_ERROR, "o2net: crypto_ahash_setkey failed: err %d", ret);
		goto failed;
	}

	shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm), GFP_KERNEL);
	if (!shash) {
		ret = -ENOMEM;
		goto failed;
	}

	shash->tfm = tfm;

	ret = crypto_shash_digest(shash, plaintext, psize, output);

	kfree(shash);

failed:
	crypto_free_shash(tfm);
	return ret;
}

static int pkcs5_pbkdf2_hmac(const u8 *key, u8 ksize,
		const u8 *salt, u8 saltlen, int iter, u8 *out)
{
	int i, j, ret;
	int dlen = SHA256_DIGEST_SIZE;
	u8 *p = out;
	u8 output[SHA256_DIGEST_SIZE] = {0};
	u8 digtmp[SHA256_DIGEST_SIZE] = {0};

	if (!ksize)
		return 0;

	ret = hmac_sha256(key, ksize, salt, saltlen, output);
	if (ret)
		return ret;

	memcpy(digtmp, output, dlen);
	memcpy(p, output, dlen);

	for (i = 1; i < iter; i++) {
		ret = hmac_sha256(key, ksize, digtmp, dlen, output);
		if (ret)
			return ret;
		memcpy(digtmp, output, dlen);
		for (j = 0; j < dlen; j++)
			p[j] ^= digtmp[j];
	}

	return ret;
}


/* 0-legal, other-illegal */
static int o2net_check_token(struct o2net_token *token,
		struct o2net_node *nn,
		struct o2net_sock_container *sc)
{
	int ret = 0;
	u64 time_stamp;
	u8 credential[SHA256_DIGEST_SIZE] = {0};
	u8 key[CRYPTO_KEY_LEN_MAX + 1] = {0};
	u8 derive_key[SHA256_DIGEST_SIZE + 1] = {0};
	u8 plaintext[O2NET_PLAINTEXT_LEN] = {0};
	u8 psize = 0;
	struct o2nm_node *node = NULL;

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	if (sc && nn->nn_sc == sc)
		node = sc->sc_node;
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	if (!node)
		return -EINVAL;

	time_stamp = be64_to_cpu(token->time_stamp);
	if (time_stamp == nn->nn_time_stamp) {
		mlog(ML_ERROR, "o2net: time_stamp is the same as before!\n");
		return -EINVAL;
	}
	nn->nn_time_stamp = time_stamp;

	/* assemble for token */
	o2net_get_crypto_key(key);
	ret = pkcs5_pbkdf2_hmac(key, strlen(key), token->nonce,
			O2NET_NONCE_LEN, O2NET_PBKDF2_ITER, derive_key);
	if (ret)
		return ret;

	psize += snprintf(plaintext + psize, O2NET_PLAINTEXT_LEN - psize,
		"%zu ", sizeof(struct o2net_token));
	if (node->nd_ipnet_type == IPV4_TYPE)
		psize += snprintf(plaintext + psize, O2NET_NONCE_LEN - psize,
			"%pI4 ", &node->nd_ipv4_address);
	else
		psize += snprintf(plaintext + psize, O2NET_NONCE_LEN - psize,
			"%pI6 ", node->nd_ipv6_address);
	psize += snprintf(plaintext + psize, O2NET_PLAINTEXT_LEN - psize, "%llu", time_stamp);
	ret = pkcs5_pbkdf2_hmac(derive_key, strlen(derive_key),
			plaintext, sizeof(plaintext), O2NET_PBKDF2_ITER, credential);
	if (ret)
		return ret;

	if (memcmp(token->credential, credential, SHA256_DIGEST_SIZE)) {
		static unsigned long caller_jiffies;

		if (printk_timed_ratelimit(&caller_jiffies,
				LOG_O2NET_CRYPTO_RATELIMIT_INTERVAL_MSES))
			mlog(ML_ERROR, "Invalid Token from Node %u.\n", sc->sc_node->nd_num);
		ret = -EINVAL;
	} else {
		mlog(ML_NOTICE, "Accept Token from Node %u.\n", sc->sc_node->nd_num);
	}

	return ret;
}

static int o2net_check_handshake(struct o2net_sock_container *sc)
{
	int ret;
	void *data;
	size_t datalen;
	struct o2net_handshake *hand = page_address(sc->sc_page);
	struct o2net_handshake_token *hand_token = page_address(sc->sc_page);
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);

	if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: " SC_NODEF_FMT
				" advertised net protocol version %llu but %llu is required, disconnecting\n",
				SC_NODEF_ARGS(sc),
				(unsigned long long)be64_to_cpu(hand->protocol_version),
				O2NET_PROTOCOL_VERSION);
		else
			mlog(ML_NOTICE, "o2net: " SC_IPV6_NODEF_FMT
				" advertised net protocol version %llu but %llu is required, disconnecting\n",
				SC_IPV6_NODEF_ARGS(sc),
				(unsigned long long)be64_to_cpu(hand->protocol_version),
				O2NET_PROTOCOL_VERSION);

		/* don't bother reconnecting if its the wrong version. */
		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be64_to_cpu(hand->connector_id) == O2NET_CONNECT_ID_V2) {
		if (!o2net_get_key_compat_switch()) {
			mlog(ML_ERROR, "o2net: incompatible with the handshake without token!\n");
			o2net_ensure_shutdown(nn, sc, -ENOTCONN);
			return -1;
		}
		nn->o2net_msg_version = O2NET_MSG;
		atomic_set(&nn->nn_connector_id, O2NET_CONNECT_ID_V2);
	} else if (be64_to_cpu(hand->connector_id) == O2NET_CONNECT_ID_V3) {
		nn->o2net_msg_version = O2NET_MSG;
		atomic_set(&nn->nn_connector_id, O2NET_CONNECT_ID_V3);
		data = page_address(sc->sc_page) + sc->sc_page_off;
		datalen = sizeof(struct o2net_handshake_token) - sc->sc_page_off;
		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
		if (ret > 0) {
			sc->sc_page_off += ret;
		} else {
			o2net_ensure_shutdown(nn, sc, -ENOTCONN);
			return -1;
		}

		if (sc->sc_page_off == sizeof(struct o2net_handshake_token)) {
			ret = o2net_check_token(&hand_token->token, nn, sc);
			if (ret) {
				o2net_ensure_shutdown(nn, sc, -ENOTCONN);
				return -1;
			}
		} else {
			/* there is more data to receive and let the caller retry */
			return 0;
		}
	} else {
		/*
		 * old version without token will run into this branch
		 * when receive handshake token. And the caller will
		 * return -EINVAL which cause the new version retry.
		 */
		nn->o2net_msg_version = O2NET_MSG_V1;
	}

	/*
	 * Ensure timeouts are consistent with other nodes, otherwise
	 * we can end up with one node thinking that the other must be down,
	 * but isn't. This can ultimately cause corruption.
	 */
	if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
				o2net_idle_timeout()) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: " SC_NODEF_FMT
			     " uses a network idle timeout of %u ms, but we use %u ms locally.  disconnecting\n",
			     SC_NODEF_ARGS(sc),
			     be32_to_cpu(hand->o2net_idle_timeout_ms),
			     o2net_idle_timeout());
		else
			mlog(ML_NOTICE, "o2net: " SC_IPV6_NODEF_FMT
			     " uses a network idle timeout of %u ms, but we use %u ms locally.  disconnecting\n",
			     SC_IPV6_NODEF_ARGS(sc),
			     be32_to_cpu(hand->o2net_idle_timeout_ms),
			     o2net_idle_timeout());
		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
			o2net_keepalive_delay()) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: " SC_NODEF_FMT
			     " uses a keepalive delay of %u ms, but we use %u ms locally.  disconnecting\n",
			     SC_NODEF_ARGS(sc),
			     be32_to_cpu(hand->o2net_keepalive_delay_ms),
			     o2net_keepalive_delay());
		else
			mlog(ML_NOTICE, "o2net: " SC_IPV6_NODEF_FMT
			     " uses a keepalive delay of %u ms, but we use %u ms locally.  disconnecting\n",
			     SC_IPV6_NODEF_ARGS(sc),
			     be32_to_cpu(hand->o2net_keepalive_delay_ms),
			     o2net_keepalive_delay());
		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be32_to_cpu(hand->o2hb_heartbeat_fake_timeout_ms) !=
			O2HB_WRITE_FAKE_TIMEOUT_MS) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: " SC_NODEF_FMT
					" uses a fake heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_fake_timeout_ms),
					O2HB_WRITE_FAKE_TIMEOUT_MS);
		else
			mlog(ML_NOTICE, "o2net: " SC_IPV6_NODEF_FMT
					" uses a fake heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_fake_timeout_ms),
					O2HB_WRITE_FAKE_TIMEOUT_MS);
		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be32_to_cpu(hand->o2hb_heartbeat_real_timeout_ms) !=
			O2HB_WRITE_REAL_TIMEOUT_MS) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: " SC_NODEF_FMT
					" uses a real heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_real_timeout_ms),
					O2HB_WRITE_REAL_TIMEOUT_MS);
		else
			mlog(ML_NOTICE, "o2net: " SC_IPV6_NODEF_FMT
					" uses a real heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_real_timeout_ms),
					O2HB_WRITE_REAL_TIMEOUT_MS);
		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	sc->sc_handshake_ok = 1;

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	/* set valid and queue the idle timers only if it hasn't been
	 * shut down already */
	if (nn->nn_sc == sc) {
		o2net_sc_reset_idle_timer(sc);
		atomic_set(&nn->nn_timeout, 0);
		o2net_set_nn_state(nn, sc, 1, 0);
	}
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	/* shift everything up as though it wasn't there */
	if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V2) {
		sc->sc_page_off -= sizeof(struct o2net_handshake);
		if (sc->sc_page_off)
			memmove(hand, hand + 1, sc->sc_page_off);
	} else if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V3) {
		sc->sc_page_off -= sizeof(struct o2net_handshake_token);
		if (sc->sc_page_off)
			memmove(hand_token, hand_token + 1, sc->sc_page_off);
	} else {
		mlog(ML_ERROR, "o2net: o2net node connector id %d is invalid\n",
				atomic_read(&nn->nn_connector_id));
	}

	return 0;
}

static int o2net_pre_check_handshake(struct o2net_sock_container *sc)
{
	int ret;
	void *data;
	size_t datalen;
	struct o2net_handshake *hand = page_address(sc->sc_page);
	struct o2net_handshake_token *hand_token = page_address(sc->sc_page);
	struct o2net_node *nn = o2net_pre_nn_from_num(sc->sc_node->nd_num);

	if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: " SC_NODEF_FMT
					" advertised net protocol version %llu but %llu is required, disconnecting\n",
					SC_NODEF_ARGS(sc),
					(unsigned long long)be64_to_cpu(hand->protocol_version),
					O2NET_PROTOCOL_VERSION);
		else
			mlog(ML_NOTICE, "o2net_pre: " SC_IPV6_NODEF_FMT
					" advertised net protocol version %llu but %llu is required, disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					(unsigned long long)be64_to_cpu(hand->protocol_version),
					O2NET_PROTOCOL_VERSION);

		/* don't bother reconnecting if its the wrong version. */
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be64_to_cpu(hand->connector_id) == O2NET_CONNECT_ID_V2) {
		if (!o2net_get_key_compat_switch()) {
			mlog(ML_ERROR,
				"o2net_pre: incompatible with the handshake without token!\n");
			o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
			return -1;
		}
		nn->o2net_msg_version = O2NET_MSG;
		atomic_set(&nn->nn_connector_id, O2NET_CONNECT_ID_V2);
	} else if (be64_to_cpu(hand->connector_id) == O2NET_CONNECT_ID_V3) {
		nn->o2net_msg_version = O2NET_MSG;
		atomic_set(&nn->nn_connector_id, O2NET_CONNECT_ID_V3);
		data = page_address(sc->sc_page) + sc->sc_page_off;
		datalen = sizeof(struct o2net_handshake_token) - sc->sc_page_off;
		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
		if (ret > 0) {
			sc->sc_page_off += ret;
		} else {
			o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
			return -1;
		}

		if (sc->sc_page_off == sizeof(struct o2net_handshake_token)) {
			ret = o2net_check_token(&hand_token->token, nn, sc);
			if (ret) {
				o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
				return -1;
			}
		} else {
			/* there is more data to receive and let the caller retry */
			return 0;
		}
	} else
		nn->o2net_msg_version = O2NET_MSG_V1;

	/*
	 * Ensure timeouts are consistent with other nodes, otherwise
	 * we can end up with one node thinking that the other must be down,
	 * but isn't. This can ultimately cause corruption.
	 */
	if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
				o2net_idle_timeout()) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: " SC_NODEF_FMT
					" uses a network idle timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2net_idle_timeout_ms),
					o2net_idle_timeout());
		else
			mlog(ML_NOTICE, "o2net_pre: " SC_IPV6_NODEF_FMT
					" uses a network idle timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2net_idle_timeout_ms),
					o2net_idle_timeout());
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
			o2net_keepalive_delay()) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: " SC_NODEF_FMT
					" uses a keepalive delay of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2net_keepalive_delay_ms),
					o2net_keepalive_delay());
		else
			mlog(ML_NOTICE, "o2net_pre: " SC_IPV6_NODEF_FMT
					" uses a keepalive delay of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2net_keepalive_delay_ms),
					o2net_keepalive_delay());
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be32_to_cpu(hand->o2hb_heartbeat_fake_timeout_ms) !=
			O2HB_WRITE_FAKE_TIMEOUT_MS) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: " SC_NODEF_FMT
					" uses a fake heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_fake_timeout_ms),
					O2HB_WRITE_FAKE_TIMEOUT_MS);
		else
			mlog(ML_NOTICE, "o2net_pre: " SC_IPV6_NODEF_FMT
					" uses a fake heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_fake_timeout_ms),
					O2HB_WRITE_FAKE_TIMEOUT_MS);
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	if (be32_to_cpu(hand->o2hb_heartbeat_real_timeout_ms) !=
			O2HB_WRITE_REAL_TIMEOUT_MS) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: " SC_NODEF_FMT
					" uses a real heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_real_timeout_ms),
					O2HB_WRITE_REAL_TIMEOUT_MS);
		else
			mlog(ML_NOTICE, "o2net_pre: " SC_IPV6_NODEF_FMT
					" uses a real heartbeat timeout of %u ms, but we use %u ms locally.  disconnecting\n",
					SC_IPV6_NODEF_ARGS(sc),
					be32_to_cpu(hand->o2hb_heartbeat_real_timeout_ms),
					O2HB_WRITE_REAL_TIMEOUT_MS);
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
		return -1;
	}

	sc->sc_handshake_ok = 1;

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	/* set valid and queue the idle timers only if it hasn't been
	 * shut down already */
	if (nn->nn_sc == sc) {
		o2net_pre_sc_reset_idle_timer(sc);
		o2net_pre_set_nn_state(nn, sc, 1, 0);
	}
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	/* shift everything up as though it wasn't there */
	if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V2) {
		sc->sc_page_off -= sizeof(struct o2net_handshake);
		if (sc->sc_page_off)
			memmove(hand, hand + 1, sc->sc_page_off);
	} else if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V3) {
		sc->sc_page_off -= sizeof(struct o2net_handshake_token);
		if (sc->sc_page_off)
			memmove(hand_token, hand_token + 1, sc->sc_page_off);
	} else {
		mlog(ML_ERROR, "o2net_pre: o2net node connector id %d is invalid\n",
				atomic_read(&nn->nn_connector_id));
	}

	return 0;
}

static int o2net_pre_advance_rx_v2(struct o2net_sock_container *sc)
{
	struct o2net_msg *hdr;
	int ret = 0, response = 0;
	void *data;
	size_t datalen;

	/* do we need more header? */
	if (sc->sc_page_off < sizeof(struct o2net_msg)) {
		data = page_address(sc->sc_page) + sc->sc_page_off;
		datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
		if (ret > 0) {
			sc->sc_page_off += ret;
			/* only swab incoming here.. we can
			 * only get here once as we cross from
			 * being under to over */
			if (sc->sc_page_off == sizeof(struct o2net_msg)) {
				hdr = page_address(sc->sc_page);
				if (be16_to_cpu(hdr->data_len) >
						O2NET_MAX_PAYLOAD_BYTES)
					ret = -EOVERFLOW;
			}
		}
		if (ret <= 0)
			goto out;
	}

	if (sc->sc_page_off < sizeof(struct o2net_msg)) {
		/* oof, still don't have a header */
		goto out;
	}

	/* this was swabbed above when we first read it */
	hdr = page_address(sc->sc_page);

	msglog_level(ML_MSG, hdr, "at page_off %zu\n", sc->sc_page_off);

	/* do we need more payload? */
	if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
		/* need more payload */
		data = page_address(sc->sc_page) + sc->sc_page_off;
		datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
				sc->sc_page_off;
		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
		if (ret > 0)
			sc->sc_page_off += ret;
		if (ret <= 0)
			goto out;
	}

	if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
		struct o2net_node *nn = o2net_pre_nn_from_num(sc->sc_node->nd_num);

		o2net_pre_sc_postpone_idle(sc);

		switch (be16_to_cpu(hdr->magic)) {
		case O2NET_MSG_STATUS_MAGIC:
			ret = o2net_check_crc16(nn, hdr, sc->sc_node->nd_num);
			if (ret) {
				msglog_level(ML_ERROR, hdr,
						"bad crc from node[%u], returned %d\n",
						sc->sc_node->nd_num, ret);
				goto out;
			}

			o2net_complete_nsw(nn, NULL, be32_to_cpu(hdr->msg_num),
					be32_to_cpu(hdr->sys_status),
					be32_to_cpu(hdr->status));
			ret = 0;
			break;
		case O2NET_MSG_MAGIC:
			ret = o2net_check_crc16(nn, hdr, sc->sc_node->nd_num);
			if (ret) {
				msglog_level(ML_ERROR, hdr,
						"bad crc from node[%u], returned %d\n",
						sc->sc_node->nd_num, ret);
				goto out;
			}

			response = o2hb_query_handler(hdr);

			mutex_lock(&sc->sc_send_lock);
			ret = o2net_send_status_magic(sc->sc_sock, hdr, O2NET_ERR_NONE,
					response);
			mutex_unlock(&sc->sc_send_lock);
			break;
		default:
			ret = -EINVAL;
			msglog_level(ML_ERROR, hdr,
				"bad magic from node[%u] returned %d\n",
				sc->sc_node->nd_num, ret);
			break;
		}

		if (ret == 0)
			ret = 1;
		sc->sc_page_off = 0;
	}

out:
	return ret;
}

static int o2net_advance_rx_v2(struct o2net_sock_container *sc)
{
	struct o2net_msg *hdr;
	int ret = 0;
	void *data;
	size_t datalen;

	/* do we need more header? */
	if (sc->sc_page_off < sizeof(struct o2net_msg)) {
		data = page_address(sc->sc_page) + sc->sc_page_off;
		datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
		if (ret > 0) {
			sc->sc_page_off += ret;
			/* only swab incoming here.. we can
			 * only get here once as we cross from
			 * being under to over */
			if (sc->sc_page_off == sizeof(struct o2net_msg)) {
				hdr = page_address(sc->sc_page);
				if (be16_to_cpu(hdr->data_len) >
				    O2NET_MAX_PAYLOAD_BYTES)
					ret = -EOVERFLOW;
			}
		}
		if (ret <= 0)
			goto out;
	}

	if (sc->sc_page_off < sizeof(struct o2net_msg)) {
		/* oof, still don't have a header */
		goto out;
	}

	/* this was swabbed above when we first read it */
	hdr = page_address(sc->sc_page);

	msglog_level(ML_MSG, hdr, "at page_off %zu\n", sc->sc_page_off);

	/* do we need more payload? */
	if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
		/* need more payload */
		data = page_address(sc->sc_page) + sc->sc_page_off;
		datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
			  sc->sc_page_off;
		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
		if (ret > 0)
			sc->sc_page_off += ret;
		if (ret <= 0)
			goto out;
	}

	if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
		/* we can only get here once, the first time we read
		 * the payload.. so set ret to progress if the handler
		 * works out. after calling this the message is toast */
		ret = o2net_process_message(sc, hdr);
		if (ret == 0)
			ret = 1;
		sc->sc_page_off = 0;
	}

out:
	sclog(sc, "ret = %d\n", ret);
	o2net_set_advance_stop_time(sc);
	return ret;
}

static int o2net_pre_advance_rx(struct o2net_sock_container *sc)
{
	int ret = 0;
	void *data;
	size_t datalen;
	struct o2net_node *nn = o2net_pre_nn_from_num(sc->sc_node->nd_num);

	if (unlikely(sc->sc_handshake_ok == 0)) {
		if (sc->sc_page_off < sizeof(struct o2net_handshake)) {
			data = page_address(sc->sc_page) + sc->sc_page_off;
			datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
			ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
			if (ret > 0)
				sc->sc_page_off += ret;
		}

		if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
			o2net_pre_check_handshake(sc);
			if (unlikely(sc->sc_handshake_ok == 0))
				ret = -EPROTO;
		}
		goto out;
	}

	if (nn->o2net_msg_version == O2NET_MSG) {
		ret = o2net_pre_advance_rx_v2(sc);
	} else {
		mlog(ML_ERROR, "o2net_pre: invalid msg version: %d\n", nn->o2net_msg_version);
		ret = -EINVAL;
	}
out:
	return ret;
}

static void o2net_pre_rx_until_empty(struct work_struct *work)
{
	struct o2net_sock_container *sc =
			container_of(work, struct o2net_sock_container, sc_pre_rx_work);
	int ret;

	do {
		ret = o2net_pre_advance_rx(sc);
	} while (ret > 0);

	if (ret <= 0 && ret != -EAGAIN) {
		struct o2net_node *nn = o2net_pre_nn_from_num(sc->sc_node->nd_num);

		sclog(sc, "saw error %d, closing\n", ret);
		/* not permanent so read failed handshake can retry */
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);
	}

	sc_put(sc);
}

/* this demuxes the queued rx bytes into header or payload bits and calls
 * handlers as each full message is read off the socket.  it returns -error,
 * == 0 eof, or > 0 for progress made.*/
static int o2net_advance_rx(struct o2net_sock_container *sc)
{
	int ret = 0;
	void *data;
	size_t datalen;
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);

	sclog(sc, "receiving\n");
	o2net_set_advance_start_time(sc);

	if (unlikely(sc->sc_handshake_ok == 0)) {
		if (sc->sc_page_off < sizeof(struct o2net_handshake)) {
			data = page_address(sc->sc_page) + sc->sc_page_off;
			datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
			ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
			if (ret > 0)
				sc->sc_page_off += ret;
		}

		if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
			o2net_check_handshake(sc);
			if (unlikely(sc->sc_handshake_ok == 0))
				ret = -EPROTO;
		}
		goto out;
	}

	if (nn->o2net_msg_version == O2NET_MSG) {
		ret = o2net_advance_rx_v2(sc);
	} else {
		mlog(ML_ERROR, "o2net: invalid msg version: %d\n", nn->o2net_msg_version);
		ret = -EINVAL;
	}

out:
	return ret;
}

/* this work func is triggerd by data ready.  it reads until it can read no
 * more.  it interprets 0, eof, as fatal.  if data_ready hits while we're doing
 * our work the work struct will be marked and we'll be called again. */
static void o2net_rx_until_empty(struct work_struct *work)
{
	struct o2net_sock_container *sc =
		container_of(work, struct o2net_sock_container, sc_rx_work);
	int ret;

	do {
		ret = o2net_advance_rx(sc);
	} while (ret > 0);

	if (ret <= 0 && ret != -EAGAIN) {
		struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
		sclog(sc, "saw error %d, closing\n", ret);
		/* not permanent so read failed handshake can retry */
		o2net_ensure_shutdown(nn, sc, 0);
	}

	sc_put(sc);
}

static int o2net_set_nodelay(struct socket *sock)
{
	tcp_sock_set_nodelay(sock->sk);
	return 0;
}

static int o2net_set_usertimeout(struct socket *sock)
{
	tcp_sock_set_user_timeout(sock->sk, msecs_to_jiffies(O2NET_TCP_USER_TIMEOUT));
	return 0;
}

static int o2net_set_reuseport(struct socket *sock)
{
	sock_set_reuseport(sock->sk);
	return 0;
}

static void o2net_initialize_handshake(void)
{
	o2net_hand->o2hb_heartbeat_fake_timeout_ms = cpu_to_be32(
		O2HB_WRITE_FAKE_TIMEOUT_MS);
	o2net_hand->o2hb_heartbeat_real_timeout_ms = cpu_to_be32(
		O2HB_WRITE_REAL_TIMEOUT_MS);
	o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
	o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32(
		o2net_keepalive_delay());
	o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32(
		o2net_reconnect_delay());
}

static void o2net_initialize_handshake_token(void)
{
	u64 time_stamp = ktime_get_real_seconds();
	u8 nonce[O2NET_NONCE_LEN] = {0};
	u8 credential[SHA256_DIGEST_SIZE] = {0};
	u8 key[CRYPTO_KEY_LEN_MAX + 1] = {0};
	u8 derive_key[SHA256_DIGEST_SIZE + 1] = {0};
	u8 plaintext[O2NET_PLAINTEXT_LEN] = {0};
	u8 psize = 0;
	struct o2nm_node *node = o2nm_get_node_by_num(o2nm_this_node());

	if (!node)
		return;

	o2net_hand_token->hand.o2hb_heartbeat_fake_timeout_ms = cpu_to_be32(
		O2HB_WRITE_FAKE_TIMEOUT_MS);
	o2net_hand_token->hand.o2hb_heartbeat_real_timeout_ms = cpu_to_be32(
		O2HB_WRITE_REAL_TIMEOUT_MS);
	o2net_hand_token->hand.o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
	o2net_hand_token->hand.o2net_keepalive_delay_ms = cpu_to_be32(
		o2net_keepalive_delay());
	o2net_hand_token->hand.o2net_reconnect_delay_ms = cpu_to_be32(
		o2net_reconnect_delay());

	/* assemble for token */
	o2net_hand_token->token.time_stamp = cpu_to_be64(time_stamp);
	get_random_bytes(nonce, O2NET_NONCE_LEN);
	memcpy(o2net_hand_token->token.nonce, nonce, O2NET_NONCE_LEN);
	o2net_get_crypto_key(key);
	pkcs5_pbkdf2_hmac(key, strlen(key), nonce, O2NET_NONCE_LEN,
			O2NET_PBKDF2_ITER, derive_key);

	psize += snprintf(plaintext + psize, O2NET_PLAINTEXT_LEN - psize,
			"%zu ", sizeof(struct o2net_token));
	if (node->nd_ipnet_type == IPV4_TYPE)
		psize += snprintf(plaintext + psize, O2NET_NONCE_LEN - psize,
				"%pI4 ", &node->nd_ipv4_address);
	else
		psize += snprintf(plaintext + psize, O2NET_NONCE_LEN - psize,
				"%pI6 ", node->nd_ipv6_address);
	psize += snprintf(plaintext + psize, O2NET_PLAINTEXT_LEN - psize, "%llu", time_stamp);
	pkcs5_pbkdf2_hmac(derive_key, strlen(derive_key), plaintext, sizeof(plaintext),
				O2NET_PBKDF2_ITER, credential);
	memcpy(o2net_hand_token->token.credential, credential, SHA256_DIGEST_SIZE);
	o2nm_node_put(node);
}

/* ------------------------------------------------------------ */

/* called when a connect completes and after a sock is accepted.  the
 * rx path will see the response and mark the sc valid */
static void o2net_sc_connect_completed(struct work_struct *work)
{
	struct o2net_sock_container *sc =
		container_of(work, struct o2net_sock_container,
			     sc_connect_work);
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);

	mlog(ML_MSG, "o2net: sc sending handshake with ver %llu id %x\n",
			(unsigned long long)O2NET_PROTOCOL_VERSION,
			atomic_read(&nn->nn_connector_id));

	if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V2) {
		o2net_initialize_handshake();
		o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
	} else if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V3) {
		o2net_initialize_handshake_token();
		o2net_sendpage(sc, o2net_hand_token, sizeof(*o2net_hand_token));
	}

	sc_put(sc);
}

/* this is called as a work_struct func. */
static void o2net_sc_send_keep_req(struct work_struct *work)
{
	struct o2net_sock_container *sc =
		container_of(work, struct o2net_sock_container,
			     sc_keepalive_work.work);

	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);

	if (nn->o2net_msg_version == O2NET_MSG)
		o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
	sc_put(sc);
}

static void o2net_pre_sc_connect_completed(struct work_struct *work)
{
	struct o2net_sock_container *sc =
		container_of(work, struct o2net_sock_container,
				sc_pre_connect_work);
	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);

	mlog(ML_NOTICE, "o2net_pre: sc sending handshake with ver %llu id %x\n",
			(unsigned long long)O2NET_PROTOCOL_VERSION,
			atomic_read(&nn->nn_connector_id));

	if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V2) {
		o2net_initialize_handshake();
		o2net_pre_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
	} else if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V3) {
		o2net_initialize_handshake_token();
		o2net_pre_sendpage(sc, o2net_hand_token, sizeof(*o2net_hand_token));
	}

	sc_put(sc);
}

static void o2net_pre_idle_timer(struct timer_list *t)
{
	struct o2net_sock_container *sc = from_timer(sc, t, sc_idle_timeout);

#ifdef CONFIG_DEBUG_FS
	ktime_t now = ktime_get();
#endif

	if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
		mlog(ML_NOTICE, "o2net_pre: connection to " SC_NODEF_FMT
				" has been idle for %u.%u seconds, shutting it down.\n",
				SC_PRE_NODEF_ARGS(sc),
				o2net_idle_timeout() / 1000,
				o2net_idle_timeout() % 1000);
	else
		mlog(ML_NOTICE, "o2net_pre: connection to " SC_IPV6_NODEF_FMT
				" has been idle for %u.%u seconds, shutting it down.\n",
				SC_IPV6_PRE_NODEF_ARGS(sc),
				o2net_idle_timeout() / 1000,
				o2net_idle_timeout() % 1000);

#ifdef CONFIG_DEBUG_FS
	mlog(ML_NOTICE, "o2net_pre: Here are some times that might help debug the "
			"situation: (Timer: %lld, Now %lld, DataReady %lld, Advance %lld-%lld, "
			"Key 0x%08x, Func %u, FuncTime %lld-%lld)\n",
			(long long)ktime_to_us(sc->sc_tv_timer), (long long)ktime_to_us(now),
			(long long)ktime_to_us(sc->sc_tv_data_ready),
			(long long)ktime_to_us(sc->sc_tv_advance_start),
			(long long)ktime_to_us(sc->sc_tv_advance_stop),
			sc->sc_msg_key, sc->sc_msg_type,
			(long long)ktime_to_us(sc->sc_tv_func_start),
			(long long)ktime_to_us(sc->sc_tv_func_stop));
#endif

	o2net_pre_sc_queue_work(sc, &sc->sc_pre_shutdown_work);
}

static void o2net_pre_sc_reset_idle_timer(struct o2net_sock_container *sc)
{
	o2net_set_sock_timer(sc);
	mod_timer(&sc->sc_idle_timeout,
			jiffies + msecs_to_jiffies(o2net_idle_timeout()));
}

static void o2net_pre_sc_postpone_idle(struct o2net_sock_container *sc)
{
	/* Only push out an existing timer */
	if (timer_pending(&sc->sc_idle_timeout))
		o2net_pre_sc_reset_idle_timer(sc);
}

/* socket shutdown does a del_timer_sync against this as it tears down.
 * we can't start this timer until we've got to the point in sc buildup
 * where shutdown is going to be involved */
static void o2net_idle_timer(struct timer_list *t)
{
	struct o2net_sock_container *sc = from_timer(sc, t, sc_idle_timeout);

#ifdef CONFIG_DEBUG_FS
	ktime_t now = ktime_get();
#endif

	if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
		mlog(ML_NOTICE, "o2net: connection to " SC_NODEF_FMT
			     " has been idle for %u.%u seconds, shutting it down.\n",
			     SC_NODEF_ARGS(sc),
			     o2net_idle_timeout() / 1000,
			     o2net_idle_timeout() % 1000);
	else
		mlog(ML_NOTICE, "o2net: connection to " SC_IPV6_NODEF_FMT
			     " has been idle for %u.%u seconds, shutting it down.\n",
			     SC_IPV6_NODEF_ARGS(sc),
			     o2net_idle_timeout() / 1000,
			     o2net_idle_timeout() % 1000);

#ifdef CONFIG_DEBUG_FS
	mlog(ML_NOTICE, "o2net: Here are some times that might help debug the "
	     "situation: (Timer: %lld, Now %lld, DataReady %lld, Advance %lld-%lld, "
	     "Key 0x%08x, Func %u, FuncTime %lld-%lld)\n",
	     (long long)ktime_to_us(sc->sc_tv_timer), (long long)ktime_to_us(now),
	     (long long)ktime_to_us(sc->sc_tv_data_ready),
	     (long long)ktime_to_us(sc->sc_tv_advance_start),
	     (long long)ktime_to_us(sc->sc_tv_advance_stop),
	     sc->sc_msg_key, sc->sc_msg_type,
	     (long long)ktime_to_us(sc->sc_tv_func_start),
	     (long long)ktime_to_us(sc->sc_tv_func_stop));
#endif

	o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
}

static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
{
	o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
	o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
		      msecs_to_jiffies(o2net_keepalive_delay()));
	o2net_set_sock_timer(sc);
	mod_timer(&sc->sc_idle_timeout,
	       jiffies + msecs_to_jiffies(o2net_idle_timeout()));
}

static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
{
	/* Only push out an existing timer */
	if (timer_pending(&sc->sc_idle_timeout))
		o2net_sc_reset_idle_timer(sc);

}

int o2net_pre_start_connect(int node_num)
{
	struct o2net_node *nn = o2net_pre_nn_from_num(node_num);
	struct o2net_sock_container *sc = NULL;
	struct o2nm_node *node = NULL, *mynode = NULL;
	struct socket *sock = NULL;
	struct sockaddr *mysa = NULL, *remotesa = NULL;
	struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
	struct sockaddr_in6 myaddr6 = {0, }, remoteaddr6 = {0, };
	unsigned short sa_family;
	int ret = 0, set_nn = 0, addr_len;
	unsigned int noio_flag;

	noio_flag = memalloc_noio_save();

	/* watch for racing with tearing a node down */
	node = o2nm_get_node_by_num(o2net_pre_num_from_nn(nn));
	if (node == NULL) {
		mlog(ML_ERROR, "o2net_pre: node %d does not exist!\n",
				o2net_pre_num_from_nn(nn));
		ret = -EINVAL;
		goto out;
	}

	mynode = o2nm_get_node_by_num(o2nm_this_node());
	if (mynode == NULL) {
		mlog(ML_ERROR, "o2net_pre: my node does not exist!\n");
		ret = -EINVAL;
		goto out;
	}

	sc = sc_pre_alloc(node);
	if (sc == NULL) {
		mlog(ML_ERROR, "o2net_pre: couldn't allocate sc\n");
		ret = -ENOMEM;
		goto out;
	}

	if (mynode->nd_ipnet_type == IPV4_TYPE) {
		myaddr.sin_family = AF_INET;
		sa_family = PF_INET;
		myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
		myaddr.sin_port = mynode->nd_ip_port;
		mysa = (struct sockaddr *)&myaddr;

		remoteaddr.sin_family = AF_INET;
		remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
		remoteaddr.sin_port = htons(ntohs(node->nd_ip_port) + 1);
		remotesa = (struct sockaddr *)&remoteaddr;
		addr_len = sizeof(myaddr);
	} else {
		myaddr6.sin6_family = AF_INET6;
		sa_family = PF_INET6;
		memcpy(&myaddr6.sin6_addr, mynode->nd_ipv6_address,
				sizeof(mynode->nd_ipv6_address));
		myaddr6.sin6_port = mynode->nd_ip_port;
		mysa = (struct sockaddr *)&myaddr6;

		remoteaddr6.sin6_family = AF_INET6;
		memcpy(&remoteaddr6.sin6_addr, node->nd_ipv6_address,
				sizeof(node->nd_ipv6_address));
		remoteaddr6.sin6_port = htons(ntohs(node->nd_ip_port) + 1);
		remotesa = (struct sockaddr *)&remoteaddr6;
		addr_len = sizeof(myaddr6);
	}

	ret = sock_create(sa_family, SOCK_STREAM, IPPROTO_TCP, &sock);
	if (ret < 0) {
		mlog(ML_ERROR, "o2net_pre: can't create socket: %d\n", ret);
		goto out;
	}
	sc->sc_sock = sock; /* freed by sc_kref_release */

	sock->sk->sk_allocation = GFP_ATOMIC;

	/*set SO_REUSEPORT */
	ret = o2net_set_reuseport(sock);
	if (ret < 0) {
		if (mynode->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI4:%u, ret = %d\n",
				__func__, &mynode->nd_ipv4_address,
				ntohs(mynode->nd_ip_port), ret);
		} else {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI6:%u, ret = %d\n",
				__func__, mynode->nd_ipv6_address,
				ntohs(mynode->nd_ip_port), ret);
		}
		goto out;
	}

	ret = sock->ops->bind(sock, mysa, addr_len);
	if (ret) {
		if (mynode->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR, "o2net_pre: bind failed with %d at address %pI4\n",
					ret, &mynode->nd_ipv4_address);
		} else {
			mlog(ML_ERROR, "o2net_pre: bind failed with %d at address %pI6\n",
					ret, mynode->nd_ipv6_address);
		}
		goto out;
	}

	ret = o2net_set_nodelay(sc->sc_sock);
	if (ret) {
		mlog(ML_ERROR, "o2net_pre: setting TCP_NODELAY failed with %d\n", ret);
		goto out;
	}

	ret = o2net_set_usertimeout(sock);
	if (ret) {
		mlog(ML_ERROR, "o2net_pre: set TCP_USER_TIMEOUT failed with %d\n", ret);
		goto out;
	}

	o2net_pre_register_callbacks(sc->sc_sock->sk, sc);

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	if (nn->nn_sc) {
		spin_unlock(&nn->nn_lock);
		mutex_unlock(&nn->nn_mutex);
		o2net_pre_sc_queue_work(sc, &sc->sc_pre_shutdown_work);
		ret = -EBUSY;
		goto out;
	}
	o2net_pre_set_nn_state(nn, sc, 0, 0);
	set_nn = 1;
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	ret = sc->sc_sock->ops->connect(sc->sc_sock,
			remotesa, addr_len, O_NONBLOCK);
	if (ret == -EINPROGRESS)
		ret = 0;

out:
	if (ret && sc) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net_pre: connect attempt to " SC_NODEF_FMT
					" failed ith errno %d\n", SC_PRE_NODEF_ARGS(sc), ret);
		else
			mlog(ML_NOTICE, "o2net_pre: connect attempt to " SC_IPV6_NODEF_FMT
					" failed with errno %d\n", SC_IPV6_PRE_NODEF_ARGS(sc), ret);
		o2net_pre_ensure_shutdown(nn, sc, -ENOTCONN);

		/*
		 * When pre_check for LUNA and LUNB concurrenttly, LUNB
		 * will find 'nn->nn_sc' already existed and cancel LUNA's
		 * 'nn_pre_connect_expired'. This makes LUNA waiting for
		 * 'o2net_tx_can_proceed()' in o2net_pre_send_message_vec()
		 * forever. Furthermore it causes mount hung. So we should
		 * not cancel last 'nn_pre_connect_expired'.
		 */
		if (set_nn && o2net_pre_wq) {
			cancel_delayed_work(&nn->nn_pre_connect_expired);
			flush_workqueue(o2net_pre_wq);
		}
	}
	if (sc)
		sc_put(sc);
	if (node)
		o2nm_node_put(node);
	if (mynode)
		o2nm_node_put(mynode);

	memalloc_noio_restore(noio_flag);

	return ret;
}

/* this work func is kicked whenever a path sets the nn state which doesn't
 * have valid set.  This includes seeing hb come up, losing a connection,
 * having a connect attempt fail, etc. This centralizes the logic which decides
 * if a connect attempt should be made or if we should give up and all future
 * transmit attempts should fail */
static void o2net_start_connect(struct work_struct *work)
{
	struct o2net_node *nn =
		container_of(work, struct o2net_node, nn_connect_work.work);
	struct o2net_sock_container *sc = NULL;
	struct o2nm_node *node = NULL, *mynode = NULL;
	struct socket *sock = NULL;
	struct sockaddr *mysa = NULL, *remotesa = NULL;
	struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
	struct sockaddr_in6 myaddr6 = {0, }, remoteaddr6 = {0, };
	unsigned short sa_family;
	int ret = 0, stop, addr_len;
	unsigned int timeout;
	unsigned int noio_flag;

	/*
	 * sock_create allocates the sock with GFP_KERNEL. We must set
	 * per-process flag PF_MEMALLOC_NOIO so that all allocations done
	 * by this process are done as if GFP_NOIO was specified. So we
	 * are not reentering filesystem while doing memory reclaim.
	 */
	noio_flag = memalloc_noio_save();
	/* if we're greater we initiate tx, otherwise we accept */
	if (o2nm_this_node() <= o2net_num_from_nn(nn))
		goto out;

	/* watch for racing with tearing a node down */
	node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
	if (node == NULL)
		goto out;

	mynode = o2nm_get_node_by_num(o2nm_this_node());
	if (mynode == NULL)
		goto out;

	spin_lock(&nn->nn_lock);
	/*
	 * see if we already have one pending or have given up.
	 * For nn_timeout, it is set when we close the connection
	 * because of the idle time out. So it means that we have
	 * at least connected to that node successfully once,
	 * now try to connect to it again.
	 */
	timeout = atomic_read(&nn->nn_timeout);
	stop = (nn->nn_sc ||
		(nn->nn_persistent_error &&
		(nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
	spin_unlock(&nn->nn_lock);
	mlog(ML_CONN,
			"o2net: %s connect to %u, sc %p, timeout %d, error %d\n",
			stop ? "Stop" : "Start",
			o2net_num_from_nn(nn), nn->nn_sc, atomic_read(&nn->nn_timeout),
			nn->nn_persistent_error);
	if (stop)
		goto out;

	nn->nn_last_connect_attempt = jiffies;

	sc = sc_alloc(node);
	if (sc == NULL) {
		mlog(0, "o2net: couldn't allocate sc\n");
		ret = -ENOMEM;
		goto out;
	}

	if (mynode->nd_ipnet_type == IPV4_TYPE) {
		myaddr.sin_family = AF_INET;
		sa_family = PF_INET;
		myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
		myaddr.sin_port = mynode->nd_ip_port;
		mysa = (struct sockaddr *)&myaddr;

		remoteaddr.sin_family = AF_INET;
		remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
		remoteaddr.sin_port = node->nd_ip_port;
		remotesa = (struct sockaddr *)&remoteaddr;
		addr_len = sizeof(myaddr);
	} else {
		myaddr6.sin6_family = AF_INET6;
		sa_family = PF_INET6;
		memcpy(&myaddr6.sin6_addr, mynode->nd_ipv6_address,
				sizeof(mynode->nd_ipv6_address));
		myaddr6.sin6_port = mynode->nd_ip_port;
		mysa = (struct sockaddr *)&myaddr6;

		remoteaddr6.sin6_family = AF_INET6;
		memcpy(&remoteaddr6.sin6_addr, node->nd_ipv6_address,
				sizeof(node->nd_ipv6_address));
		remoteaddr6.sin6_port = node->nd_ip_port;
		remotesa = (struct sockaddr *)&remoteaddr6;
		addr_len = sizeof(myaddr6);
	}

	ret = sock_create(sa_family, SOCK_STREAM, IPPROTO_TCP, &sock);
	if (ret < 0) {
		mlog(0, "o2net: can't create socket: %d\n", ret);
		goto out;
	}
	sc->sc_sock = sock; /* freed by sc_kref_release */

	sock->sk->sk_allocation = GFP_ATOMIC;

	/*set SO_REUSEPORT */
	ret = o2net_set_reuseport(sock);
	if (ret < 0) {
		if (mynode->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI4:%u, ret = %d\n",
				__func__,  &mynode->nd_ipv4_address,
				ntohs(mynode->nd_ip_port), ret);
		} else {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI6:%u, ret = %d\n",
				__func__, mynode->nd_ipv6_address,
				ntohs(mynode->nd_ip_port), ret);
		}
		goto out;
	}

	ret = sock->ops->bind(sock, mysa, addr_len);
	if (ret) {
		if (mynode->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR, "o2net: bind failed with %d at address %pI4\n",
			     ret, &mynode->nd_ipv4_address);
		} else {
			mlog(ML_ERROR, "o2net: bind failed with %d at address %pI6\n",
			     ret, mynode->nd_ipv6_address);
		}
		goto out;
	}

	ret = o2net_set_nodelay(sc->sc_sock);
	if (ret) {
		mlog(ML_ERROR, "o2net: setting TCP_NODELAY failed with %d\n", ret);
		goto out;
	}

	ret = o2net_set_usertimeout(sock);
	if (ret) {
		mlog(ML_ERROR, "o2net: set TCP_USER_TIMEOUT failed with %d\n", ret);
		goto out;
	}

	o2net_register_callbacks(sc->sc_sock->sk, sc);

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	/* handshake completion will set nn->nn_sc_valid */
	o2net_set_nn_state(nn, sc, 0, 0);
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	ret = sc->sc_sock->ops->connect(sc->sc_sock,
					remotesa, addr_len, O_NONBLOCK);
	if (ret == -EINPROGRESS)
		ret = 0;

out:
	if (ret && sc) {
		if (sc->sc_node->nd_ipnet_type == IPV4_TYPE)
			mlog(ML_NOTICE, "o2net: connect attempt to " SC_NODEF_FMT
				" failed with errno %d\n", SC_NODEF_ARGS(sc), ret);
		else
			mlog(ML_NOTICE, "o2net: connect attempt to " SC_IPV6_NODEF_FMT
				" failed with errno %d\n", SC_IPV6_NODEF_ARGS(sc), ret);
		/* 0 err so that another will be queued and attempted
		 * from set_nn_state */
		o2net_ensure_shutdown(nn, sc, 0);
	}
	if (sc)
		sc_put(sc);
	if (node)
		o2nm_node_put(node);
	if (mynode)
		o2nm_node_put(mynode);

	memalloc_noio_restore(noio_flag);
	return;
}

static void o2net_connect_expired(struct work_struct *work)
{
	struct o2net_node *nn =
		container_of(work, struct o2net_node, nn_connect_expired.work);

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	if (!nn->nn_sc_valid) {
		mlog(ML_ERROR, "o2net: no connection established with node %u after "
		     "%u.%u seconds, check network and cluster configuration.\n",
		     o2net_num_from_nn(nn),
		     o2net_idle_timeout() / 1000,
		     o2net_idle_timeout() % 1000);

		o2net_set_nn_state(nn, NULL, 0, 0);
	}
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);
}

static void o2net_pre_connect_expired(struct work_struct *work)
{
	struct o2net_node *nn =
		container_of(work, struct o2net_node, nn_pre_connect_expired.work);

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	if (!nn->nn_sc_valid) {
		mlog(ML_ERROR, "o2net_pre: no connection established with node %u after "
				"%u.%u seconds, giving up and returning errors.\n",
				o2net_pre_num_from_nn(nn),
				o2net_idle_timeout() / 1000,
				o2net_idle_timeout() % 1000);

		o2net_pre_set_nn_state(nn, NULL, 0, -ENOTCONN);
	}
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);
}

static void o2net_still_up(struct work_struct *work)
{
	struct o2net_node *nn =
		container_of(work, struct o2net_node, nn_still_up.work);

	mlog(ML_CONN, "o2net: node %u still up, sc %p\n",
			o2net_num_from_nn(nn), nn->nn_sc);
	o2quo_hb_still_up(o2net_num_from_nn(nn));
}

/* ------------------------------------------------------------ */

void o2net_disconnect_node(struct o2nm_node *node)
{
	struct o2net_node *nn = o2net_nn_from_num(node->nd_num);

	/* don't reconnect until it's heartbeating again */
	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	o2net_delete_all_seqnum_status(nn);

	atomic_set(&nn->nn_timeout, 0);
	o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
	/* re-init nn_connector_id here in case of
	 * reconnecting node without token */
	atomic_set(&nn->nn_connector_id, O2NET_CONNECT_ID_V3);
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	if (o2net_wq) {
		cancel_delayed_work(&nn->nn_connect_expired);
		cancel_delayed_work(&nn->nn_connect_work);
		cancel_delayed_work(&nn->nn_still_up);
		flush_workqueue(o2net_wq);
	}
}

static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
				  void *data, u64 generation)
{
	if (!node)
		return;

	if (node_num != o2nm_this_node())
		o2net_disconnect_node(node);

	BUG_ON(atomic_read(&o2net_connected_peers) < 0);
}

void o2net_connect_node(struct o2nm_node *node)
{
	struct o2net_node *nn = o2net_nn_from_num(node->nd_num);

	if (node->nd_num == o2nm_this_node() ||
			!o2hb_check_node_heartbeating_from_callback(node->nd_num, NULL))
		return;

	/* ensure an immediate connect attempt */
	nn->nn_last_connect_attempt = jiffies -
			(msecs_to_jiffies(o2net_reconnect_delay()) + 1);

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	atomic_set(&nn->nn_timeout, 0);
	if (nn->nn_persistent_error)
		o2net_set_nn_state(nn, NULL, 0, 0);
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);
}

void o2net_connect_all_node(struct o2nm_node *node)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
		struct o2nm_node *node = o2nm_get_node_by_num(i);

		if (node) {
			o2net_connect_node(node);
			o2nm_node_put(node);
		}
	}
}

static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
				void *data, u64 generation)
{
	struct o2net_node *nn = o2net_nn_from_num(node_num);

	BUG_ON(!node);

	/* ensure an immediate connect attempt */
	nn->nn_last_connect_attempt = jiffies -
		(msecs_to_jiffies(o2net_reconnect_delay()) + 1);

	if (node_num != o2nm_this_node()) {
		/* believe it or not, accept and node heartbeating testing
		 * can succeed for this node before we got here.. so
		 * only use set_nn_state to clear the persistent error
		 * if that hasn't already happened */
		mutex_lock(&nn->nn_mutex);
		spin_lock(&nn->nn_lock);
		atomic_set(&nn->nn_timeout, 0);
		if (nn->nn_persistent_error)
			o2net_set_nn_state(nn, NULL, 0, 0);
		spin_unlock(&nn->nn_lock);
		mutex_unlock(&nn->nn_mutex);
	}
}

void o2net_unregister_hb_callbacks(void)
{
	o2hb_unregister_callback(NULL, &o2net_hb_up);
	o2hb_unregister_callback(NULL, &o2net_hb_down);
}

int o2net_register_hb_callbacks(void)
{
	int ret;

	o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
			    o2net_hb_node_down_cb, NULL, O2NET_HB_PRI, NULL);
	o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
			    o2net_hb_node_up_cb, NULL, O2NET_HB_PRI, NULL);

	ret = o2hb_register_callback(NULL, &o2net_hb_up);
	if (ret == 0)
		ret = o2hb_register_callback(NULL, &o2net_hb_down);

	if (ret)
		o2net_unregister_hb_callbacks();

	return ret;
}

/* ------------------------------------------------------------ */

static int o2net_pre_accept_one(struct socket *sock)
{
	int ret;
	struct sockaddr *sa = NULL;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
	struct socket *new_sock = NULL;
	struct o2nm_node *node = NULL;
	struct o2nm_node *local_node = o2nm_get_node_by_num(o2nm_this_node());
	struct o2net_sock_container *sc = NULL;
	struct o2net_node *nn;
	unsigned int noio_flag;

	noio_flag = memalloc_noio_save();

	BUG_ON(sock == NULL);

	if (!local_node)
		return -EINVAL;

	ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
			sock->sk->sk_protocol, &new_sock);
	if (ret)
		goto out;

	new_sock->type = sock->type;
	new_sock->ops = sock->ops;
	ret = sock->ops->accept(sock, new_sock, O_NONBLOCK, false);
	if (ret < 0)
		goto out;

	new_sock->sk->sk_allocation = GFP_ATOMIC;

	ret = o2net_set_nodelay(new_sock);
	if (ret) {
		mlog(ML_ERROR, "o2net_pre: setting TCP_NODELAY failed with %d\n", ret);
		goto out;
	}

	ret = o2net_set_usertimeout(new_sock);
	if (ret) {
		mlog(ML_ERROR, "o2net_pre: set TCP_USER_TIMEOUT failed with %d\n", ret);
		goto out;
	}

	if (local_node->nd_ipnet_type == IPV4_TYPE)
		sa = (struct sockaddr *)&sin;
	else
		sa = (struct sockaddr *)&sin6;

	ret = new_sock->ops->getname(new_sock, sa, 1);
	if (ret < 0)
		goto out;

	if (local_node->nd_ipnet_type == IPV4_TYPE) {
		node = o2nm_get_node_by_ipv4(sin.sin_addr.s_addr);
		if (node == NULL) {
			mlog(ML_ERROR,
					"o2net_pre: attempt to connect from unknown node at %pI4:%d\n",
					&sin.sin_addr.s_addr, ntohs(sin.sin_port));
			ret = -EINVAL;
			goto out;
		}
	} else {
		node = o2nm_get_node_by_ipv6(sin6.sin6_addr.s6_addr);
		if (node == NULL) {
			mlog(ML_ERROR,
					"o2net_pre: attempt to connect from unknown node at %pI6:%d\n",
					sin6.sin6_addr.s6_addr, ntohs(sin6.sin6_port));
			ret = -EINVAL;
			goto out;
		}
	}

	nn = o2net_pre_nn_from_num(node->nd_num);

	sc = sc_pre_alloc(node);
	if (sc == NULL) {
		ret = -ENOMEM;
		goto out;
	}

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	if (nn->nn_sc) {
		spin_unlock(&nn->nn_lock);
		mutex_unlock(&nn->nn_mutex);
		ret = -EBUSY;
		if (local_node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
					"o2net_pre: attempt to connect from node '%s' at %pI4:%d but it already has an open connection\n",
					node->nd_name, &sin.sin_addr.s_addr,
					ntohs(sin.sin_port));
		} else {
			mlog(ML_ERROR,
					"o2net_pre: attempt to connect from node '%s' at %pI6:%d but it already has an open connection\n",
					node->nd_name, sin6.sin6_addr.s6_addr,
					ntohs(sin6.sin6_port));
		}
		goto out;
	}

	ret = 0;
	sc->sc_sock = new_sock;
	new_sock = NULL;
	o2net_pre_set_nn_state(nn, sc, 0, 0);
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	o2net_pre_register_callbacks(sc->sc_sock->sk, sc);
	o2net_pre_sc_queue_work(sc, &sc->sc_pre_rx_work);

	if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V2) {
		o2net_initialize_handshake();
		o2net_pre_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
	} else if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V3) {
		o2net_initialize_handshake_token();
		o2net_pre_sendpage(sc, o2net_hand_token, sizeof(*o2net_hand_token));
	}

out:
	if (new_sock)
		sock_release(new_sock);
	if (node)
		o2nm_node_put(node);
	if (local_node)
		o2nm_node_put(local_node);
	if (sc)
		sc_put(sc);

	memalloc_noio_restore(noio_flag);
	return ret;
}

static void o2net_pre_accept_many(struct work_struct *work)
{
	struct socket *sock = o2net_pre_listen_sock;

	while (o2net_pre_accept_one(sock) == 0)
		cond_resched();
}

static void o2net_pre_listen_data_ready(struct sock *sk)
{
	void (*ready)(struct sock *sk);

	read_lock_bh(&sk->sk_callback_lock);
	ready = sk->sk_user_data;
	if (ready == NULL) { /* check for teardown race */
		ready = sk->sk_data_ready;
		goto out;
	}

	/* This callback may called twice when a new connection
	 * is  being established as a child socket inherits everything
	 * from a parent LISTEN socket, including the data_ready cb of
	 * the parent. This leads to a hazard. In o2net_accept_one()
	 * we are still initializing the child socket but have not
	 * changed the inherited data_ready callback yet when
	 * data starts arriving.
	 * We avoid this hazard by checking the state.
	 * For the listening socket,  the state will be TCP_LISTEN; for the new
	 * socket, will be  TCP_ESTABLISHED. Also, in this case,
	 * sk->sk_user_data is not a valid function pointer.
	 */
	if (sk->sk_state == TCP_LISTEN)
		queue_work(o2net_pre_wq, &o2net_pre_listen_work);
	else
		ready = NULL;

out:
	read_unlock_bh(&sk->sk_callback_lock);
	if (ready != NULL)
		ready(sk);
}

static int o2net_pre_open_listening_sock(struct o2nm_node *node)
{
	struct socket *sock = NULL;
	int ret, addr_len;
	struct sockaddr *sa = NULL;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
	unsigned short sa_family, port = ntohs(node->nd_ip_port) + 1;

	if (node->nd_ipnet_type == IPV4_TYPE) {
		sin.sin_family = sa_family = PF_INET;
		sin.sin_addr.s_addr = node->nd_ipv4_address;
		sin.sin_port = htons(port);
		sa = (struct sockaddr *)&sin;
		addr_len = sizeof(sin);
	} else {
		sin6.sin6_family = sa_family = PF_INET6;
		memcpy(&sin6.sin6_addr, node->nd_ipv6_address, sizeof(sin6.sin6_addr));
		sin6.sin6_port = htons(port);
		sa = (struct sockaddr *)&sin6;
		addr_len = sizeof(sin6);
	}
	ret = sock_create(sa_family, SOCK_STREAM, IPPROTO_TCP, &sock);
	if (ret < 0) {
		mlog(ML_ERROR, "o2net_pre: unable to create socket, ret=%d\n", ret);
		goto out;
	}

	sock->sk->sk_allocation = GFP_ATOMIC;

	write_lock_bh(&sock->sk->sk_callback_lock);
	sock->sk->sk_user_data = sock->sk->sk_data_ready;
	sock->sk->sk_data_ready = o2net_pre_listen_data_ready;
	write_unlock_bh(&sock->sk->sk_callback_lock);

	o2net_pre_listen_sock = sock;
	INIT_WORK(&o2net_pre_listen_work, o2net_pre_accept_many);

	/*set SO_REUSEPORT */
	ret = o2net_set_reuseport(sock);
	if (ret < 0) {
		if (node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI4:%u, ret = %d\n",
				__func__, &sin.sin_addr.s_addr, port, ret);
		} else {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI6:%u, ret = %d\n",
				__func__, sin6.sin6_addr.s6_addr, port, ret);
		}
		goto out;
	}

	sock->sk->sk_reuse = 1;
	ret = sock->ops->bind(sock, sa, addr_len);
	if (ret < 0) {
		if (node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
				"o2net_pre: unable to bind socket at %pI4:%u, ret=%d\n",
				&sin.sin_addr.s_addr, port, ret);
		} else {
			mlog(ML_ERROR,
				"o2net_pre: unable to bind socket at %pI6:%u, ret=%d\n",
				sin6.sin6_addr.s6_addr, port, ret);
		}
		goto out;
	}

	ret = sock->ops->listen(sock, 64);
	if (ret < 0) {
		if (node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR, "o2net_pre: unable to listen on %pI4:%u, ret=%d\n",
					&sin.sin_addr.s_addr, port, ret);
		} else {
			mlog(ML_ERROR, "o2net_pre: unable to listen on %pI6:%u, ret=%d\n",
					sin6.sin6_addr.s6_addr, port, ret);
		}
	}

out:
	if (ret) {
		o2net_pre_listen_sock = NULL;
		if (sock)
			sock_release(sock);
	}
	return ret;
}

int o2net_pre_start_listening(struct o2nm_node *node, bool change_ip)
{
	int ret = 0;

	BUG_ON(!change_ip && o2net_pre_wq != NULL);
	BUG_ON(o2net_pre_listen_sock != NULL);

	if (!change_ip) {
		mlog(ML_KTHREAD, "starting o2net_pre thread...\n");
		o2net_pre_wq = alloc_ordered_workqueue("o2net_pre", WQ_MEM_RECLAIM);
		if (o2net_pre_wq == NULL) {
			mlog(ML_ERROR, "o2net_pre: unable to launch o2net_pre thread\n");
			return -ENOMEM; /* ? */
		}
	}

	ret = o2net_pre_open_listening_sock(node);
	if (ret) {
		if (!change_ip) {
			destroy_workqueue(o2net_pre_wq);
			o2net_pre_wq = NULL;
		}
	}

	return ret;
}

void o2net_pre_stop_listening(struct o2nm_node *node, bool change_ip)
{
	struct socket *sock = o2net_pre_listen_sock;
	size_t i;

	if (!o2net_pre_wq || !o2net_pre_listen_sock)
		return;

	/* stop the listening socket from generating work */
	write_lock_bh(&sock->sk->sk_callback_lock);
	sock->sk->sk_data_ready = sock->sk->sk_user_data;
	sock->sk->sk_user_data = NULL;
	write_unlock_bh(&sock->sk->sk_callback_lock);

	for (i = 0; i < ARRAY_SIZE(o2net_pre_nodes); i++) {
		struct o2nm_node *node = o2nm_get_node_by_num(i);

		if (node) {
			o2net_pre_shutdown_socket(i);
			o2nm_node_put(node);
		}
	}

	if (!change_ip) {
		/* finish all work and tear down the work queue */
		mlog(ML_KTHREAD, "waiting for o2net_pre thread to exit....\n");
		destroy_workqueue(o2net_pre_wq);
		o2net_pre_wq = NULL;
	}

	sock_release(o2net_pre_listen_sock);
	o2net_pre_listen_sock = NULL;
}

static int o2net_accept_one(struct socket *sock, int *more)
{
	int ret;
	struct sockaddr *sa = NULL;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
	struct socket *new_sock = NULL;
	struct o2nm_node *node = NULL;
	struct o2nm_node *local_node = o2nm_get_node_by_num(o2nm_this_node());
	struct o2net_sock_container *sc = NULL;
	struct o2net_node *nn;
	unsigned int noio_flag;

	/*
	 * sock_create_lite allocates the sock with GFP_KERNEL. We must set
	 * per-process flag PF_MEMALLOC_NOIO so that all allocations done
	 * by this process are done as if GFP_NOIO was specified. So we
	 * are not reentering filesystem while doing memory reclaim.
	 */
	noio_flag = memalloc_noio_save();

	BUG_ON(sock == NULL);

	if (!local_node)
		return -EINVAL;

	*more = 0;
	ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
			       sock->sk->sk_protocol, &new_sock);
	if (ret)
		goto out;

	new_sock->type = sock->type;
	new_sock->ops = sock->ops;
	ret = sock->ops->accept(sock, new_sock, O_NONBLOCK, false);
	if (ret < 0)
		goto out;

	*more = 1;
	new_sock->sk->sk_allocation = GFP_ATOMIC;

	ret = o2net_set_nodelay(new_sock);
	if (ret) {
		mlog(ML_ERROR, "o2net: setting TCP_NODELAY failed with %d\n", ret);
		goto out;
	}

	ret = o2net_set_usertimeout(new_sock);
	if (ret) {
		mlog(ML_ERROR, "o2net: set TCP_USER_TIMEOUT failed with %d\n", ret);
		goto out;
	}

	if (local_node->nd_ipnet_type == IPV4_TYPE)
		sa = (struct sockaddr *)&sin;
	else
		sa = (struct sockaddr *)&sin6;

	ret = new_sock->ops->getname(new_sock, sa, 1);
	if (ret < 0)
		goto out;

	if (local_node->nd_ipnet_type == IPV4_TYPE) {
		node = o2nm_get_node_by_ipv4(sin.sin_addr.s_addr);
		if (node == NULL) {
			mlog(ML_NOTICE, "o2net: attempt to connect from unknown node at %pI4:%d\n",
			     &sin.sin_addr.s_addr, ntohs(sin.sin_port));
			ret = -EINVAL;
			goto out;
		}
	} else {
		node = o2nm_get_node_by_ipv6(sin6.sin6_addr.s6_addr);
		if (node == NULL) {
			mlog(ML_NOTICE, "o2net: attempt to connect from unknown node at %pI6:%d\n",
			     sin6.sin6_addr.s6_addr, ntohs(sin6.sin6_port));
			ret = -EINVAL;
			goto out;
		}
	}

	if (o2nm_this_node() >= node->nd_num) {
		if (local_node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_NOTICE,
					"o2net: unexpected connect attempt seen at node '%s' (%u, %pI4:%d) from node '%s' (%u, %pI4:%d)\n",
					local_node->nd_name, local_node->nd_num,
					&(local_node->nd_ipv4_address),
					ntohs(local_node->nd_ip_port),
					node->nd_name, node->nd_num, &sin.sin_addr.s_addr,
					ntohs(sin.sin_port));
		} else {
			mlog(ML_NOTICE,
					"o2net: unexpected connect attempt seen at node '%s' (%u, %pI6:%d) from node '%s' (%u, %pI6:%d)\n",
					local_node->nd_name, local_node->nd_num,
					local_node->nd_ipv6_address,
					ntohs(local_node->nd_ip_port),
					node->nd_name, node->nd_num, sin6.sin6_addr.s6_addr,
					ntohs(sin6.sin6_port));
		}
		ret = -EINVAL;
		goto out;
	}

	/* this happens all the time when the other node sees our heartbeat
	 * and tries to connect before we see their heartbeat */
	if (!o2hb_check_node_heartbeating_from_callback(node->nd_num, NULL)) {
		if (local_node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
			     "o2net: attempt to connect from node '%s' at %pI4:%d but it isn't heartbeating\n",
			     node->nd_name, &sin.sin_addr.s_addr,
			     ntohs(sin.sin_port));
		} else {
			mlog(ML_ERROR,
			     "o2net: attempt to connect from node '%s' at %pI6:%d but it isn't heartbeating\n",
			     node->nd_name, sin6.sin6_addr.s6_addr,
			     ntohs(sin6.sin6_port));
		}
		ret = -EINVAL;
		goto out;
	}

	nn = o2net_nn_from_num(node->nd_num);

	spin_lock(&nn->nn_lock);
	if (nn->nn_sc)
		ret = -EBUSY;
	else
		ret = 0;
	spin_unlock(&nn->nn_lock);
	if (ret) {
		if (local_node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_NOTICE,
			     "o2net: attempt to connect from node '%s' at %pI4:%d but it already has an open connection\n",
			     node->nd_name, &sin.sin_addr.s_addr,
			     ntohs(sin.sin_port));
		} else {
			mlog(ML_NOTICE,
			     "o2net: attempt to connect from node '%s' at %pI6:%d but it already has an open connection\n",
			     node->nd_name, sin6.sin6_addr.s6_addr,
			     ntohs(sin6.sin6_port));
		}
		goto out;
	}

	sc = sc_alloc(node);
	if (sc == NULL) {
		ret = -ENOMEM;
		goto out;
	}

	sc->sc_sock = new_sock;
	new_sock = NULL;

	mutex_lock(&nn->nn_mutex);
	spin_lock(&nn->nn_lock);
	atomic_set(&nn->nn_timeout, 0);
	o2net_set_nn_state(nn, sc, 0, 0);
	spin_unlock(&nn->nn_lock);
	mutex_unlock(&nn->nn_mutex);

	o2net_register_callbacks(sc->sc_sock->sk, sc);
	o2net_sc_queue_work(sc, &sc->sc_rx_work);

	if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V2) {
		o2net_initialize_handshake();
		o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
	} else if (atomic_read(&nn->nn_connector_id) == O2NET_CONNECT_ID_V3) {
		o2net_initialize_handshake_token();
		o2net_sendpage(sc, o2net_hand_token, sizeof(*o2net_hand_token));
	}

out:
	if (new_sock)
		sock_release(new_sock);
	if (node)
		o2nm_node_put(node);
	if (local_node)
		o2nm_node_put(local_node);
	if (sc)
		sc_put(sc);

	memalloc_noio_restore(noio_flag);
	return ret;
}

static void o2net_accept_many(struct work_struct *work)
{
	struct socket *sock = o2net_listen_sock;
	int more;
	int err;

	for (;;) {
		err = o2net_accept_one(sock, &more);
		if (!more)
			break;
		cond_resched();
	}
}

static void o2net_listen_data_ready(struct sock *sk)
{
	void (*ready)(struct sock *sk);

	read_lock_bh(&sk->sk_callback_lock);
	ready = sk->sk_user_data;
	if (ready == NULL) { /* check for teardown race */
		ready = sk->sk_data_ready;
		goto out;
	}

	/* This callback may called twice when a new connection
	 * is  being established as a child socket inherits everything
	 * from a parent LISTEN socket, including the data_ready cb of
	 * the parent. This leads to a hazard. In o2net_accept_one()
	 * we are still initializing the child socket but have not
	 * changed the inherited data_ready callback yet when
	 * data starts arriving.
	 * We avoid this hazard by checking the state.
	 * For the listening socket,  the state will be TCP_LISTEN; for the new
	 * socket, will be  TCP_ESTABLISHED. Also, in this case,
	 * sk->sk_user_data is not a valid function pointer.
	 */

	if (sk->sk_state == TCP_LISTEN) {
		queue_work(o2net_wq, &o2net_listen_work);
	} else {
		ready = NULL;
	}

out:
	read_unlock_bh(&sk->sk_callback_lock);
	if (ready != NULL)
		ready(sk);
}

static int o2net_open_listening_sock(struct o2nm_node *node)
{
	struct socket *sock = NULL;
	int ret, addr_len;
	struct sockaddr *sa = NULL;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
	unsigned short sa_family;

	if (node->nd_ipnet_type == IPV4_TYPE) {
		sin.sin_family = sa_family = PF_INET;
		sin.sin_addr.s_addr = node->nd_ipv4_address;
		sin.sin_port = node->nd_ip_port;
		sa = (struct sockaddr *) &sin;
		addr_len = sizeof(sin);
	} else {
		sin6.sin6_family = sa_family = PF_INET6;
		memcpy(&sin6.sin6_addr, node->nd_ipv6_address, sizeof(sin6.sin6_addr));
		sin6.sin6_port = node->nd_ip_port;
		sa = (struct sockaddr *) &sin6;
		addr_len = sizeof(sin6);
	}

	ret = sock_create(sa_family, SOCK_STREAM, IPPROTO_TCP, &sock);
	if (ret < 0) {
		mlog(ML_ERROR, "o2net: unable to create socket, ret=%d\n", ret);
		goto out;
	}

	sock->sk->sk_allocation = GFP_ATOMIC;

	write_lock_bh(&sock->sk->sk_callback_lock);
	sock->sk->sk_user_data = sock->sk->sk_data_ready;
	sock->sk->sk_data_ready = o2net_listen_data_ready;
	write_unlock_bh(&sock->sk->sk_callback_lock);

	o2net_listen_sock = sock;
	INIT_WORK(&o2net_listen_work, o2net_accept_many);

	/*set SO_REUSEPORT */
	ret = o2net_set_reuseport(sock);
	if (ret < 0) {
		if (node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI4:%u, ret = %d\n",
				__func__, &sin.sin_addr.s_addr, ntohs(sin.sin_port), ret);
		} else {
			mlog(ML_ERROR,
				"o2net: failed to set SO_REUSEPORT in %s, local address = %pI6:%u, ret = %d\n",
				__func__, sin6.sin6_addr.s6_addr, ntohs(sin6.sin6_port), ret);
		}
		goto out;
	}

	sock->sk->sk_reuse = 1;
	ret = sock->ops->bind(sock, sa, addr_len);
	if (ret < 0) {
		if (node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR,
				"o2net: unable to bind socket at %pI4:%u, ret=%d\n",
				&sin.sin_addr.s_addr, ntohs(sin.sin_port), ret);
		} else {
			mlog(ML_ERROR,
				"o2net: unable to bind socket at %pI6:%u, ret=%d\n",
				sin6.sin6_addr.s6_addr, ntohs(sin6.sin6_port), ret);
		}
		goto out;
	}

	ret = sock->ops->listen(sock, 64);
	if (ret < 0) {
		if (node->nd_ipnet_type == IPV4_TYPE) {
			mlog(ML_ERROR, "o2net: unable to listen on %pI4:%u, ret=%d\n",
			     &sin.sin_addr.s_addr, ntohs(sin.sin_port), ret);
		} else {
			mlog(ML_ERROR, "o2net: unable to listen on %pI6:%u, ret=%d\n",
			     sin6.sin6_addr.s6_addr, ntohs(sin6.sin6_port), ret);
		}
	}

out:
	if (ret) {
		o2net_listen_sock = NULL;
		if (sock)
			sock_release(sock);
	}
	return ret;
}

/*
 * called from node manager when we should bring up our network listening
 * socket.  node manager handles all the serialization to only call this
 * once and to match it with o2net_stop_listening().  note,
 * o2nm_this_node() doesn't work yet as we're being called while it
 * is being set up.
 */
int o2net_start_listening(struct o2nm_node *node, bool change_ip)
{
	int ret = 0;

	BUG_ON(!change_ip && o2net_wq != NULL);
	BUG_ON(o2net_listen_sock != NULL);

	if (!change_ip) {
		mlog(ML_KTHREAD, "starting o2net thread...\n");
		o2net_wq = alloc_ordered_workqueue("o2net", WQ_MEM_RECLAIM);
		if (o2net_wq == NULL) {
			mlog(ML_ERROR, "o2net: unable to launch o2net thread\n");
			return -ENOMEM; /* ? */
		}
	}

	ret = o2net_open_listening_sock(node);
	if (ret) {
		if (!change_ip) {
			destroy_workqueue(o2net_wq);
			o2net_wq = NULL;
		}
	} else
		o2quo_conn_up(node->nd_num);

	return ret;
}

/* again, o2nm_this_node() doesn't work here as we're involved in
 * tearing it down */
void o2net_stop_listening(struct o2nm_node *node, bool change_ip)
{
	struct socket *sock = o2net_listen_sock;
	size_t i;

	if (!o2net_wq || !o2net_listen_sock)
		return;

	/* stop the listening socket from generating work */
	write_lock_bh(&sock->sk->sk_callback_lock);
	sock->sk->sk_data_ready = sock->sk->sk_user_data;
	sock->sk->sk_user_data = NULL;
	write_unlock_bh(&sock->sk->sk_callback_lock);

	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
		struct o2nm_node *node = o2nm_get_node_by_num(i);
		if (node) {
			o2net_disconnect_node(node);
			o2nm_node_put(node);
		}
	}

	if (!change_ip) {
		/* finish all work and tear down the work queue */
		mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
		destroy_workqueue(o2net_wq);
		o2net_wq = NULL;
	}

	sock_release(o2net_listen_sock);
	o2net_listen_sock = NULL;

	o2quo_conn_err(node->nd_num);
}

static int o2net_init_msg_cache(void)
{
	o2net_msg_cache = kmem_cache_create("o2net_msg",
		sizeof(struct o2net_seq_status),
		0, SLAB_HWCACHE_ALIGN, NULL);
	if (!o2net_msg_cache)
		return -ENOMEM;

	o2net_ack_num_cache = kmem_cache_create("o2net_ack_num",
		sizeof(struct o2net_ack_num),
		0, SLAB_HWCACHE_ALIGN, NULL);
	if (!o2net_ack_num_cache)
		return -ENOMEM;

	return 0;
}

static void o2net_destroy_msg_cache(void)
{
	kmem_cache_destroy(o2net_msg_cache);
	kmem_cache_destroy(o2net_ack_num_cache);
}

static void o2net_free_msg_list(struct o2net_seq_status_domain **msg_list)
{
	int i;

	if (msg_list) {
		for (i = 0; i < O2NET_MAX_DOMAIN_NUMBER; i++)
			kfree(msg_list[i]);
		kfree(msg_list);
	}
}
static void o2net_free_all_msg_list(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
		struct o2net_node *nn = o2net_nn_from_num(i);

		if (nn->nn_msg_list)
			o2net_free_msg_list(nn->nn_msg_list);
	}
}

static struct o2net_seq_status_domain **o2net_init_msg_list(void)
{
	int i;
	struct o2net_seq_status_domain **msg_list = NULL;

	msg_list = kcalloc(O2NET_MAX_DOMAIN_NUMBER,
			sizeof(struct o2net_seq_status_domain *), GFP_KERNEL);
	if (!msg_list)
		goto out;

	for (i = 0; i < O2NET_MAX_DOMAIN_NUMBER; i++) {
		msg_list[i] = kzalloc(sizeof(struct o2net_seq_status_domain), GFP_KERNEL);
		if (!msg_list[i])
			goto out;

		INIT_LIST_HEAD(&msg_list[i]->o2net_seq_status_list);
		INIT_LIST_HEAD(&msg_list[i]->o2net_ack_num_list);
	}
	return msg_list;

out:
	o2net_free_msg_list(msg_list);
	return NULL;
}

#define DEL_TIMEOUT  (2*60*60*1000)
#define SCAN_TIMEOUT (1*60*60*1000)

static int is_timeout(unsigned long time_start, unsigned long time_end)
{
	unsigned long diff_ms = 0;

	diff_ms = jiffies_to_msecs(time_end - time_start);
	if (diff_ms >= DEL_TIMEOUT)
		return 1;

	return 0;
}

static void del_seq_status_node(struct o2net_node *nn)
{
	int i = 0;
	unsigned long now = jiffies;
	struct o2net_seq_status *seq_status = NULL;
	struct o2net_seq_status *tmp = NULL;
	struct o2net_seq_status_domain **msg_list = nn->nn_msg_list;

	if (!msg_list)
		return;

	assert_spin_locked(&nn->nn_lock);

	for (i = 0; i < O2NET_MAX_DOMAIN_NUMBER; i++) {
		if (msg_list[i]->domain_key == 0)
			return;
		list_for_each_entry_safe(seq_status, tmp, &msg_list[i]->o2net_seq_status_list,
				o2net_seq_status_item) {
			if (!is_timeout(seq_status->time_start, now))
				break;
			list_del_init(&seq_status->o2net_seq_status_item);
			kmem_cache_free(o2net_msg_cache, seq_status);
		}
	}
}

static void o2net_scan_del_work(struct work_struct *work)
{
	int i = 0;

	mlog(ML_NOTICE, "o2net: delete the seq num which is over %d hours\n",
		DEL_TIMEOUT/(60*60*1000));
	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
		struct o2net_node *nn = o2net_nn_from_num(i);

		spin_lock(&nn->nn_lock);
		del_seq_status_node(nn);
		spin_unlock(&nn->nn_lock);
	}

	if (atomic_read(&del_seq_status_state) == NEED_DEL_SEQ_STATUS)
		schedule_delayed_work(&del_seq_status_wq, msecs_to_jiffies(SCAN_TIMEOUT));
}

static void o2net_scan_del_init(void)
{
	INIT_DELAYED_WORK(&del_seq_status_wq, o2net_scan_del_work);
	atomic_set(&del_seq_status_state, NEED_DEL_SEQ_STATUS);
	schedule_delayed_work(&del_seq_status_wq, msecs_to_jiffies(SCAN_TIMEOUT));
}

static void o2net_scan_del_exit(void)
{
	atomic_set(&del_seq_status_state, NO_NEED_DEL_SEQ_STATUS);
	cancel_delayed_work_sync(&del_seq_status_wq);
}

int o2net_init(void)
{
	unsigned long i;

	o2quo_init();

	if (o2net_debugfs_init(&o2net_nodes))
		goto out;

	o2net_hand = kzalloc(sizeof(struct o2net_handshake), GFP_KERNEL);
	o2net_hand_token = kzalloc(sizeof(struct o2net_handshake_token), GFP_KERNEL);
	o2net_keep_req = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
	o2net_keep_resp = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
	if (!o2net_hand || !o2net_hand_token || !o2net_keep_req || !o2net_keep_resp)
		goto out;

	o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
	o2net_hand->connector_id = cpu_to_be64(O2NET_CONNECT_ID_V2);
	o2net_hand_token->hand.protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
	o2net_hand_token->hand.connector_id = cpu_to_be64(O2NET_CONNECT_ID_V3);

	o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
	o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);

	for (i = 0; i < ARRAY_SIZE(o2net_pre_nodes); i++) {
		struct o2net_node *nn = o2net_pre_nn_from_num(i);

		spin_lock_init(&nn->nn_lock);
		mutex_init(&nn->nn_mutex);
		nn->nn_persistent_error = -ENOTCONN;
		init_waitqueue_head(&nn->nn_sc_wq);
		idr_init(&nn->nn_status_idr);
		INIT_LIST_HEAD(&nn->nn_status_list);
		INIT_DELAYED_WORK(&nn->nn_pre_connect_expired,
				o2net_pre_connect_expired);
	}

	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
		struct o2net_node *nn = o2net_nn_from_num(i);
		struct o2net_seq_status_domain **msg_list = NULL;

		atomic_set(&nn->nn_timeout, 0);
		atomic_set(&nn->nn_connector_id, O2NET_CONNECT_ID_V3);
		spin_lock_init(&nn->nn_lock);
		INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
		INIT_DELAYED_WORK(&nn->nn_connect_expired,
				  o2net_connect_expired);
		INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
		/* until we see hb from a node we'll return einval */
		nn->nn_persistent_error = -ENOTCONN;
		init_waitqueue_head(&nn->nn_sc_wq);
		idr_init(&nn->nn_status_idr);
		INIT_LIST_HEAD(&nn->nn_status_list);
		mutex_init(&nn->nn_mutex);
		nn->o2net_msg_version = 0;
		msg_list = o2net_init_msg_list();
		if (!msg_list)
			goto out;

		nn->nn_msg_list = msg_list;
	}

	if (o2net_init_msg_cache()) {
		mlog(ML_ERROR, "o2net: Could not create o2net_msg slab cache\n");
		goto out;
	}

	o2net_scan_del_init();

	return 0;
out:
	kfree(o2net_hand);
	kfree(o2net_hand_token);
	kfree(o2net_keep_req);
	kfree(o2net_keep_resp);

	o2net_free_all_msg_list();
	o2net_destroy_msg_cache();

	o2net_debugfs_exit();
	o2quo_exit();
	return -ENOMEM;
}

void o2net_exit(void)
{
	o2quo_exit();
	kfree(o2net_hand);
	kfree(o2net_hand_token);
	kfree(o2net_keep_req);
	kfree(o2net_keep_resp);
	o2net_debugfs_exit();
	o2net_free_all_msg_list();
	o2net_destroy_msg_cache();
	o2net_scan_del_exit();
}
